修复页面对象池问题,后续筛选逻辑直接自己实现就行,页面中不提供复杂的backup流程了

This commit is contained in:
2026-04-09 10:04:55 +08:00
parent 618084d443
commit a7ce860f56
2 changed files with 46 additions and 70 deletions
+18 -7
View File
@@ -217,7 +217,7 @@ namespace XericUI.FlipPage
protected override void Awake() protected override void Awake()
{ {
// 使用默认的 // 使用默认的
HowToCreatPageInfo = () => AddPage(); HowToCreatPageInfo = () => new PageComponentInfo();
base.Awake(); base.Awake();
@@ -231,8 +231,13 @@ namespace XericUI.FlipPage
buttoneEndPage.Target.onClick.AddListener(FlipPageToEnd); buttoneEndPage.Target.onClick.AddListener(FlipPageToEnd);
if (inputFieldFlipPageIndex.IsValidVessel()) // 翻到指定 if (inputFieldFlipPageIndex.IsValidVessel()) // 翻到指定
inputFieldFlipPageIndex.Target.onValueChanged.AddListener(s => FlipPage(s)); inputFieldFlipPageIndex.Target.onValueChanged.AddListener(s => FlipPage(s));
pageButtonPool.Init(); pageButtonPool.Initialize();
pageButtonPool.joinOnRelease += o =>
{
SwitchSelectableVesselType(o.GetComponent<Selectable>(),
button => button.onClick.RemoveAllListeners(),
toggle => toggle.onValueChanged.RemoveAllListeners());
};
// 根据组件类型创建相关内容 // 根据组件类型创建相关内容
// var prefabSelectable = pageButtonPool.Prefab.GetComponent<Selectable>(); // var prefabSelectable = pageButtonPool.Prefab.GetComponent<Selectable>();
} }
@@ -269,7 +274,6 @@ namespace XericUI.FlipPage
RefreshPageIndeButtonState(); RefreshPageIndeButtonState();
} }
public override PageInfo AddPage(PageInfo pageInfo = null, bool isFirstOtherwiseLast = false) public override PageInfo AddPage(PageInfo pageInfo = null, bool isFirstOtherwiseLast = false)
{ {
@@ -334,11 +338,16 @@ namespace XericUI.FlipPage
pageSelectButton.Add(pageIndexSelectable); pageSelectButton.Add(pageIndexSelectable);
SwitchSelectableVesselType(pageIndexSelectable.Target0, SwitchSelectableVesselType(pageIndexSelectable.Target0,
button => { button.onClick.AddListener(() => FlipPage(pageInfo.pageNumber)); }, button =>
{
button.onClick.RemoveAllListeners();
button.onClick.AddListener(() => FlipPage(pageInfo.pageNumber));
},
toggle => toggle =>
{ {
toggle.isOn = pageInfo.pageNumber == currentPageIndex; toggle.SetIsOnWithoutNotify(pageInfo.pageNumber == currentPageIndex);
toggle.group = ToggleGroup; toggle.group = ToggleGroup;
toggle.onValueChanged.RemoveAllListeners();
toggle.onValueChanged.AddListener(b => toggle.onValueChanged.AddListener(b =>
{ {
if (b) FlipPage(pageInfo.pageNumber); if (b) FlipPage(pageInfo.pageNumber);
@@ -436,6 +445,8 @@ namespace XericUI.FlipPage
} }
} }
/// <summary> /// <summary>
/// 处理可选对象类型.(如果要支持更多的类型,应优先改此方法,以便获得其他所有位置的编译器提示) /// 处理可选对象类型.(如果要支持更多的类型,应优先改此方法,以便获得其他所有位置的编译器提示)
/// </summary> /// </summary>
@@ -443,7 +454,7 @@ namespace XericUI.FlipPage
/// <param name="asButton">如果是按钮</param> /// <param name="asButton">如果是按钮</param>
/// <param name="asToggle">如果是单选项</param> /// <param name="asToggle">如果是单选项</param>
/// <returns></returns> /// <returns></returns>
protected static bool SwitchSelectableVesselType(Selectable selectable, private static bool SwitchSelectableVesselType(Selectable selectable,
Action<Button> asButton = null, Action<Button> asButton = null,
Action<Toggle> asToggle = null) Action<Toggle> asToggle = null)
{ {
@@ -73,6 +73,9 @@ namespace XericUI.FlipPage
#region 字段属性 #region 字段属性
/// <summary>
/// 页面对象池,对象池仅管理页面本身,不参与任何数据容器的管理。
/// </summary>
protected ObjectPool<PageInfo> PageInfoPool = null; protected ObjectPool<PageInfo> PageInfoPool = null;
public Func<PageInfo> HowToCreatPageInfo = () => throw new Exception("需要翻页管理器实现相应的页面创建逻辑,并重写添加默认数据方法后才可以使用。"); public Func<PageInfo> HowToCreatPageInfo = () => throw new Exception("需要翻页管理器实现相应的页面创建逻辑,并重写添加默认数据方法后才可以使用。");
@@ -337,7 +340,7 @@ namespace XericUI.FlipPage
#endif #endif
public void FlipPageToEnd() public void FlipPageToEnd()
=> FlipPage(_pageInfos.Count - 1); => FlipPage(_pageInfos.Count - 1);
/// <summary> /// <summary>
/// 添加页面在开头 /// 添加页面在开头
/// </summary> /// </summary>
@@ -347,17 +350,7 @@ namespace XericUI.FlipPage
public virtual PageInfo AddPage(PageInfo pageInfo = null, bool isFirstOtherwiseLast = false) public virtual PageInfo AddPage(PageInfo pageInfo = null, bool isFirstOtherwiseLast = false)
{ {
if (pageInfo == null) if (pageInfo == null)
{
pageInfo = PageInfoPool.Get(); pageInfo = PageInfoPool.Get();
// 执行页面生命周期
if (!GetPageInfoWithoutCollection(pageInfo))
{
Debug.LogError("创建的新页面意外的发生流程错误?");
return pageInfo;
}
}
else
GetPageInfo(pageInfo);
if (isFirstOtherwiseLast) if (isFirstOtherwiseLast)
{ {
@@ -373,6 +366,9 @@ namespace XericUI.FlipPage
// 添加页面 // 添加页面
_pageInfos.AddLast(pageInfo); _pageInfos.AddLast(pageInfo);
} }
// GetPageInfo(pageInfo);
// 执行页面生命周期
pageInfo.OnPageCreated(this);
// 刷新页面 // 刷新页面
FlipPage(currentPageIndex, true); FlipPage(currentPageIndex, true);
return pageInfo; return pageInfo;
@@ -384,10 +380,6 @@ namespace XericUI.FlipPage
/// <param name="pageInfo"></param> /// <param name="pageInfo"></param>
public virtual void RemovePage(PageInfo pageInfo) public virtual void RemovePage(PageInfo pageInfo)
{ {
// 释放页面
PageInfoPool.Release(pageInfo);
// 刷新页面
FlipPage(currentPageIndex, true);
var index = PageInfos.IndexOf(a => a == pageInfo); var index = PageInfos.IndexOf(a => a == pageInfo);
RemovePage(index); RemovePage(index);
} }
@@ -405,7 +397,7 @@ namespace XericUI.FlipPage
// 获取页面节点 // 获取页面节点
var page = _pageInfos.GetOrderNode().LandedIndex(pageIndex); var page = _pageInfos.GetOrderNode().LandedIndex(pageIndex);
// 更新后面页面的索引 // 更新删除后页面的索引
var tempPage = page; var tempPage = page;
var tempPageNumber = page.Value.pageNumber; var tempPageNumber = page.Value.pageNumber;
while (tempPage.Next != null) while (tempPage.Next != null)
@@ -414,8 +406,6 @@ namespace XericUI.FlipPage
tempPage.Value.pageNumber = tempPageNumber++; tempPage.Value.pageNumber = tempPageNumber++;
} }
// 释放页面
PageInfoPool.Release(page.Value);
// 刷新页面 // 刷新页面
FlipPage(currentPageIndex, true); FlipPage(currentPageIndex, true);
} }
@@ -460,10 +450,7 @@ namespace XericUI.FlipPage
foreach (var pageInfo in tempPageInfo) foreach (var pageInfo in tempPageInfo)
{ {
pageInfo.ItemInfos.Clear(); pageInfo.ItemInfos.Clear();
if (!resetPool) PageInfoPool.Release(pageInfo);
PageInfoPool.Release(pageInfo);
else
ReleasePageInfoWithoutCollection(pageInfo); // 直接调用释放事件
} }
ListPool<PageInfo>.Release(tempPageInfo); ListPool<PageInfo>.Release(tempPageInfo);
// 清空池 // 清空池
@@ -552,49 +539,29 @@ namespace XericUI.FlipPage
/// 定义对象池如何执行获取页面后的事件 /// 定义对象池如何执行获取页面后的事件
/// </summary> /// </summary>
/// <param name="info"></param> /// <param name="info"></param>
protected void GetPageInfo(PageInfo info) protected virtual void GetPageInfo(PageInfo info)
{ {
if (!GetPageInfoWithoutCollection(info)) // 检查代码中重复调用
if (!info.IsAlreadyRelease)
{
Debug.LogError($"流程错误,页面尝试重复获取,请检查代码流程({info.GetHashCode():X16})\n_>{info.name}");
return; return;
_pageInfos.AddLast(info); }
info.OnPageCreated(this);
return;
} }
/// <summary> /// <summary>
/// 定义对象池如何执行释放页面后的事件 /// 定义对象池如何执行释放页面后的事件
/// </summary> /// </summary>
/// <param name="info"></param> /// <param name="info"></param>
protected void ReleasePageInfo(PageInfo info) protected virtual void ReleasePageInfo(PageInfo info)
{
if (!ReleasePageInfoWithoutCollection(info))
return;
_pageInfos.Remove(info);
}
/// <summary>
/// 定义对象池如何执行获取页面后面的事件,但不包含对集合的操作
/// </summary>
/// <param name="info"></param>
protected virtual bool GetPageInfoWithoutCollection(PageInfo info)
{
if (!info.IsAlreadyRelease)
{
Debug.LogError("流程错误,页面尝试重复获取,请检查代码流程");
return false;
}
info.OnPageCreated(this);
return true;
}
/// <summary>
/// 定义对象池如何执行释放页面后的事件,但不包含对集合的操作
/// </summary>
/// <param name="info"></param>
protected virtual bool ReleasePageInfoWithoutCollection(PageInfo info)
{ {
// 检查代码中重复调用
if (info.IsAlreadyRelease) if (info.IsAlreadyRelease)
{ {
Debug.LogError("流程错误,页面尝试重复释放,请检查代码流程"); Debug.LogError("流程错误,页面尝试重复释放,请检查代码流程");
return false; return;
} }
if (CurrentPageNode != null && Currentpage == info) if (CurrentPageNode != null && Currentpage == info)
{ {
@@ -604,7 +571,7 @@ namespace XericUI.FlipPage
info.OnPageDestroy(this); info.OnPageDestroy(this);
_pageInfos.Remove(info); _pageInfos.Remove(info);
return true; return;
} }
/// <summary> /// <summary>
@@ -623,11 +590,11 @@ namespace XericUI.FlipPage
/// 添加一组数据项目(方法待优化) /// 添加一组数据项目(方法待优化)
/// </summary> /// </summary>
/// <param name="items"></param> /// <param name="items"></param>
/// <param name="customGetPage"></param> /// <param name="customPage"></param>
public virtual void AddItems(IEnumerable<ItemInfo> items, Func<PageInfo> customGetPage = null) public virtual void AddItems(IEnumerable<ItemInfo> items, PageInfo customPage = null)
{ {
foreach (var item in items) foreach (var item in items)
AddItem(item, customGetPage); AddItem(item, customPage);
} }
/// <summary> /// <summary>
@@ -637,7 +604,7 @@ namespace XericUI.FlipPage
/// <typeparam name="T">提供一个指定的数据类型,默认添加这个类型的页面</typeparam> /// <typeparam name="T">提供一个指定的数据类型,默认添加这个类型的页面</typeparam>
public virtual void AddItem<T>(ItemInfo item) public virtual void AddItem<T>(ItemInfo item)
where T : PageInfo, new() where T : PageInfo, new()
=> AddItem(item, () => new T()); => AddItem(item, new T());
/// <summary> /// <summary>
/// 添加数据项目 /// 添加数据项目
@@ -645,7 +612,7 @@ namespace XericUI.FlipPage
/// <remarks> /// <remarks>
/// 添加项目到页面中 /// 添加项目到页面中
/// </remarks> /// </remarks>
public virtual void AddItem(ItemInfo item, Func<PageInfo> customGetPage = null) public virtual void AddItem(ItemInfo item, PageInfo customPage = null)
{ {
var page = _pageInfos.Count <= 0 ? null : _pageInfos.Last.Value; var page = _pageInfos.Count <= 0 ? null : _pageInfos.Last.Value;
if (page != null && if (page != null &&
@@ -658,9 +625,8 @@ namespace XericUI.FlipPage
} }
// 新建一个页面存放数据 // 新建一个页面存放数据
page = customGetPage == null ? PageInfoPool.Get() : customGetPage(); page = customPage ?? PageInfoPool.Get();
if (customGetPage == null) AddPage(page);
_pageInfos.AddLast(page);
page.ItemInfos.Add(item); page.ItemInfos.Add(item);
_itemInfos.Add(item); _itemInfos.Add(item);
} }
@@ -767,7 +733,6 @@ namespace XericUI.FlipPage
protected internal virtual void OnPageCreated(XericFlipPageIndexVesselBase parent) protected internal virtual void OnPageCreated(XericFlipPageIndexVesselBase parent)
{ {
IsAlreadyRelease = false; IsAlreadyRelease = false;
pageNumber = 0;
} }
/// <summary> /// <summary>