TableViewComponent v2

Unity UGUI 自带的 ScrollView 控件不支持复用滚动内容,在数量大的情况下,界面容易卡顿
借鉴其他游戏控件,写了个可复用的滚动组件,扩展、优化了ScrollView

TableView 组件的基本逻辑是注册 ScrollRect 滚动事件,在滚动时实时计算位置,将移出可视区域的内容,移动到即将进入可视区域的位置,并修改内容

使用

添加游戏对象
TableViewComponent v2_第1张图片

Scroll View 添加组件
TableViewComponent v2_第2张图片

自定义脚本

实现自定义效果需要添加一个实现 ITableViewInterface 接口的脚本

public interface ITableViewInterface
{
    /// 
    /// 设置 cell 显示内容
    /// 
    /// cell 索引
    /// cell 对象
    void SetCellAtIdx(int idx, GameObject gameObject);
    /// 
    /// 每个 cell 大小
    /// 
    /// cell 索引
    /// cell 大小
    Vector2 CellSizeAtIdx(int idx);
    /// 
    /// 列表总数据量
    /// 
    /// 数据量
    int DataCount();
    /// 
    /// 列表滚动方向,需要和 ScrollRect 方向一致
    /// 
    /// 滚动方向
    TableViewComponent.TableDirection TableViewDirection();
}

添加处理 Cell 生成、回收接口(可选)

/// 
/// 在 Cell 生成后、回收前,处理 Cell 子节点
/// 
public interface ITableViewCell
{
    /// 
    /// 生成 Cell ,此时可以添加子对象
    /// 
    /// 生成的 Cell
    void OnDequeue(int idx, GameObject cell);
    /// 
    /// 回收 Cell ,此时可以回收 Cell 上的子对象
    /// 
    void OnRecycle(int idx, GameObject cell);
}

TableView 组件的公共属性和功能

/// 
/// 实现接口 ITableViewInterface 的对象
/// 
public ITableViewInterface tableView { get; set; }

/// 
/// 实现接口 ITableViewCell 的对象
/// 
public ITableViewCell tableViewCell { get; set; }

/// 
/// 是否自动加载数据,显示列表
/// 
/// 设置 false 时要手动调用  显示列表
/// 
///  大专栏  TableViewComponent v2
public bool autoLoadOnStart { get; set; } = true;

/// 
/// 返回第一个 cell 显示的区域比例(ratio) 大于 ratio 的索引
/// 
/// 索引
public int GetFirstShowRateIndex(float rate = 0.8f)

/// 
/// 返回当前显示的所有 Cell 对象
/// 
/// 当前显示的所有 Cell 对象
public List<GameObject> GetShowingCells()

/// 
/// 返回指定位置的 Cell, Cell 可能为 null
/// 
/// 索引
/// Cell
public GameObject GetCellAtIdx(int idx)

/// 
/// 是否在滚动
/// 
/// bool
public bool IsScrolling() { return DOTween.IsTweening(content); }

/// 
/// 滚动到第 idx 个 cell
/// 
/// 索引
/// 滚动时间
public void ScrollToIndex(int idx, float time = 0.5f)

/// 
/// 滚动列表完全显示第 idx 个 Cell
/// 当 Cell 完全显示时,不处理
/// 当 Cell 在列表上面时,滚动到显示区域第一个
/// 当 Cell 在列表下面时,滚动到显示区域最后一个
/// 当 Cell 面积大于整个列表的显示面积时,滚动到显示区域第一个
/// 
/// 索引
/// 滚动时间
public void ScrollToShow(int idx, float time = 0.5f)

/// 
/// 跳到第 idx 个 cell 
/// 
/// 索引
public void JumpToIndex(int idx)

/// 
/// 更新 Idx 位置的 Cell
/// Cell 的大小不变,只更新显示内容
/// 
/// 索引
public void RefreshCellAtIndex(int idx)

/// 
/// 刷新列表,保持当前位置
/// 
public void RefreshTable()

/// 
/// 刷新列表,有增删或某个 Cell 大小变化,刷新整个列表
/// 
public void ResetTable()

自定义脚本获取到 TableView 组件,设置 tableView 属性
运行时,可以调用公共方法,更新列表或跳转到某个位置

运行效果

TableViewComponent v2_第3张图片

你可能感兴趣的:(TableViewComponent v2)