com.alicizax.unity.ui.exten.../Runtime/RecyclerView/ViewHolder/ViewHolder.cs

115 lines
3.1 KiB
C#
Raw Normal View History

2025-03-12 20:59:12 +08:00
using System;
2025-04-17 16:03:39 +08:00
using AlicizaX.UI.Extension;
2025-03-12 20:59:12 +08:00
using UnityEngine;
using UnityEngine.UI;
namespace AlicizaX.UI
2025-03-12 20:59:12 +08:00
{
2026-03-11 14:18:07 +08:00
/// <summary>
/// 视图持有者基类,用于缓存和复用列表项视图
/// </summary>
2025-03-12 20:59:12 +08:00
public abstract class ViewHolder : MonoBehaviour
{
private RectTransform rectTransform;
2025-03-28 13:34:33 +08:00
2026-03-11 14:18:07 +08:00
/// <summary>
/// 获取 RectTransform 组件
/// </summary>
2025-03-12 20:59:12 +08:00
public RectTransform RectTransform
{
get
{
if (rectTransform == null)
{
rectTransform = GetComponent<RectTransform>();
}
2025-03-28 13:34:33 +08:00
2025-03-12 20:59:12 +08:00
return rectTransform;
}
2025-03-28 13:34:33 +08:00
private set { rectTransform = value; }
2025-03-12 20:59:12 +08:00
}
2026-03-11 14:18:07 +08:00
/// <summary>
/// 视图名称,用于区分不同类型的视图
/// </summary>
2025-05-30 13:43:08 +08:00
public string Name { get; internal set; }
2026-03-11 14:18:07 +08:00
/// <summary>
/// 当前绑定的数据索引
/// </summary>
2025-05-30 13:43:08 +08:00
public int Index { get; internal set; }
2025-03-12 20:59:12 +08:00
2026-03-11 14:18:07 +08:00
/// <summary>
/// 选中状态
/// </summary>
2025-04-02 14:53:08 +08:00
public bool ChoiseState { private set; get; }
2026-03-11 14:18:07 +08:00
/// <summary>
/// 获取视图的尺寸
/// </summary>
2025-03-12 20:59:12 +08:00
public Vector2 SizeDelta => RectTransform.sizeDelta;
2026-03-11 14:18:07 +08:00
2025-05-30 13:43:08 +08:00
private IButton _button;
2025-03-12 20:59:12 +08:00
2026-03-11 14:18:07 +08:00
/// <summary>
/// 视图首次创建时调用
/// </summary>
2025-05-30 13:43:08 +08:00
protected internal virtual void OnStart()
2025-03-12 20:59:12 +08:00
{
}
2026-03-11 14:18:07 +08:00
/// <summary>
/// 视图被回收到对象池时调用
/// </summary>
2025-05-30 13:43:08 +08:00
protected internal virtual void OnRecycled()
{
}
2025-03-12 20:59:12 +08:00
2026-03-11 14:18:07 +08:00
/// <summary>
/// 绑定视图数据(抽象方法,子类必须实现)
/// </summary>
/// <typeparam name="T">数据类型</typeparam>
/// <param name="data">要绑定的数据</param>
2025-06-03 20:10:36 +08:00
public abstract void BindViewData<T>(T data);
2025-05-30 13:43:08 +08:00
2026-03-11 14:18:07 +08:00
/// <summary>
/// 绑定列表项点击事件
/// </summary>
/// <typeparam name="T">数据类型</typeparam>
/// <param name="data">数据对象</param>
/// <param name="action">点击回调</param>
2025-05-30 13:43:08 +08:00
protected internal virtual void BindItemClick<T>(T data, Action<T> action)
2025-03-12 20:59:12 +08:00
{
2025-05-30 13:43:08 +08:00
if (_button is null && !TryGetComponent(out _button))
2025-03-12 20:59:12 +08:00
{
2025-05-30 13:43:08 +08:00
Log.Warning("找不到Button组件");
return;
2025-03-12 20:59:12 +08:00
}
2025-05-30 13:43:08 +08:00
_button.onClick.RemoveAllListeners();
_button.onClick.AddListener(() => action?.Invoke(data));
2025-03-12 20:59:12 +08:00
}
2026-03-11 14:18:07 +08:00
/// <summary>
/// 绑定选中状态
/// </summary>
/// <param name="state">是否选中</param>
2025-04-02 14:53:08 +08:00
protected internal void BindChoiceState(bool state)
2025-03-28 13:34:33 +08:00
{
2025-04-02 14:53:08 +08:00
if (ChoiseState != state)
{
ChoiseState = state;
OnBindChoiceState(state);
}
2025-03-28 13:34:33 +08:00
}
2025-04-01 15:21:02 +08:00
2026-03-11 14:18:07 +08:00
/// <summary>
/// 选中状态改变时的回调(可在子类中重写)
/// </summary>
/// <param name="state">是否选中</param>
2025-04-02 14:53:08 +08:00
protected internal virtual void OnBindChoiceState(bool state)
{
}
2025-03-12 20:59:12 +08:00
}
}