跳到主要内容

InfiniteCell

重要 注意 提醒

Coding Style wiki


InfiniteCellCell 的基类 (MonoBehaviour),挂载于 Cell Prefab 的根节点;Cell 由对象池循环利用,通过覆写生命周期方法实现显示逻辑,渲染所需的数据由 InfiniteCellData 承载。

命名空间OxGKit.InfiniteScrollView
类型public class InfiniteCell : MonoBehaviour
源码InfiniteCell.cs
using OxGKit.InfiniteScrollView;

提醒 通过 Right-Click Create/OxGKit/Infinite ScrollView/Template Infinite Cell.cs (Script)Template Infinite Cell (RectTransform Prefab) 可快速创建 Cell 的脚本与 Prefab 模板。

快速上手

using Cysharp.Threading.Tasks;
using OxGKit.InfiniteScrollView;
using UnityEngine.UI;

public class ItemCell : InfiniteCell
{
public Text titleTxt;
public Button button;

public override async UniTask OnCreate(object args)
{
// 创建时初始一次 (args 由 InitializePool(args) 转传)
this.button.onClick.AddListener(this.OnClick);
}

public override void OnRefresh()
{
// Cell 为对象池循环利用,显示必须完全由 cellData 驱动
string text = (string)this.cellData.data;
this.titleTxt.text = $"[{this.cellData.index}] {text}";
}

public override void OnRecycle()
{
// 回收回对象池时的清理
}

public override void OnSnap()
{
// 成为 Snap 目标时的表现 (如高亮)
}

public override void OnClick()
{
// 触发列表的 onCellSelected 事件
base.OnClick();
}
}

通用规则

生命周期

方法触发时机
OnCreateInitializePool 实例化 Cell 时 (一次性,await)。
OnRefreshcellData 被赋值时 (Cell 进入可视范围配置显示、数据刷新)。
OnRecycleCell 被回收回对象池时 (GameObject 已停用)。
OnSnapSnap 完成且该 Cell 为目标时。
OnClick点击时 (需自行将 Button 事件绑定至此方法)。

重要 Cell 为对象池循环利用 — 同一个 Cell 实例会轮流显示不同条数据,OnRefresh 必须完全由 cellData 驱动重绘,勿将单条项目的状态存于 Cell 身上 (应存于数据对象)。

注意 Insert / Remove 会位移数据索引,Cell 内请即时读取 this.cellData.index,勿缓存索引值。


成员

成员说明
public RectTransform rectTransformCell 的 RectTransform (自动缓存)。
public InfiniteCellData cellData当前绑定的数据;被赋值 (set) 时会自动触发 OnRefresh

方法

方法总览

方法说明
OnCreate创建时初始 (一次性)。
OnRefresh依数据重绘显示。
OnRecycle回收时清理。
OnSnap成为 Snap 目标时。
OnClick点击事件 (触发选中)。
InvokeSelected(protected) 主动触发选中事件。

OnCreate

public virtual async UniTask OnCreate(object args)

InitializePool 实例化 Cell 时被 await 调用一次argsInitializePool(args) 转传的自定义参数,适合进行组件初始等一次性设置。

OnRefresh

public virtual void OnRefresh()

cellData 被赋值时自动调用 — 于 Cell 被配置显示 (进入可视范围) 时触发,请在此依 cellData 重绘显示内容。

OnRecycle

public virtual void OnRecycle()

Cell 被回收回对象池时调用 (此时 GameObject 已被停用),可在此进行清理 (如停止动画、释放暂存)。

OnSnap

public virtual void OnSnap()

Snap 完成且该 Cell 为目标索引时调用 (适用页签切换、选中表现等)。

OnClick

public virtual void OnClick()

点击事件入口 — 默认实现会调用 InvokeSelected 触发列表的 onCellSelected 事件;请自行将 Cell 上的 Button 事件绑定至此方法 (覆写时记得调用 base.OnClick())。

InvokeSelected

protected void InvokeSelected()

主动触发选中事件 (通知列表的 onCellSelected);自定义点击流程 (不经由 OnClick) 时可直接调用。


InfiniteCellData

Cell 的数据载体,决定 Cell 的尺寸与渲染数据;与 Cell (显示) 分离,由列表持有并于配置显示时绑定给 Cell。

类型public class InfiniteCellData : IDisposable
源码InfiniteCellData.cs
成员说明
public int index { get; internal set; }数据索引 (由列表自动维护,Insert / Remove 后自动更新)。
public Vector2 cellSizeCell 尺寸 (x = 宽、y = 高):垂直列表取 y、水平列表取 x、Grid 两者皆用。
public object data渲染 Cell 所需的自定义数据。

构造函数

public InfiniteCellData()
public InfiniteCellData(Vector2 cellSize)
public InfiniteCellData(Vector2 cellSize, object data)

方法

public void Dispose()

释放数据引用 (data = null);由列表的 Remove / Clear 自动调用。

动态尺寸示例 (聊天室)

// 先以测量用 Text 计算文本高度,再依内容创建动态高度的数据
this.heightInstrument.text = message;
float height = this.heightInstrument.preferredHeight;
this.chatScrollView.Add(new InfiniteCellData(new Vector2(0f, height), new ChatData(user, message)));
this.chatScrollView.Refresh();
this.chatScrollView.SnapLast(0.1f);

注意 cellSize 需于加入列表前决定;之后变更尺寸需更新该条数据的 cellSize 并执行完整 Refresh