AlicizaX/Client/Packages/com.alicizax.unity.ui.extension/Runtime/RecyclerView/ViewHolder/ViewHolder.cs

70 lines
1.7 KiB
C#
Raw Normal View History

2025-04-28 19:45:45 +08:00
using System;
using AlicizaX.UI.Extension;
using UnityEngine;
using UnityEngine.UI;
namespace AlicizaX.UI.RecyclerView
{
public abstract class ViewHolder : MonoBehaviour
{
private RectTransform rectTransform;
public RectTransform RectTransform
{
get
{
if (rectTransform == null)
{
rectTransform = GetComponent<RectTransform>();
}
return rectTransform;
}
private set { rectTransform = value; }
}
2025-07-11 21:00:00 +08:00
public string Name { get; internal set; }
public int Index { get; internal set; }
2025-04-28 19:45:45 +08:00
public bool ChoiseState { private set; get; }
public Vector2 SizeDelta => RectTransform.sizeDelta;
2025-07-11 21:00:00 +08:00
private IButton _button;
2025-04-28 19:45:45 +08:00
2025-07-11 21:00:00 +08:00
protected internal virtual void OnStart()
{
}
protected internal virtual void OnRecycled()
2025-04-28 19:45:45 +08:00
{
}
public abstract void BindViewData<T>(T data);
2025-07-11 21:00:00 +08:00
protected internal virtual void BindItemClick<T>(T data, Action<T> action)
2025-04-28 19:45:45 +08:00
{
2025-07-11 21:00:00 +08:00
if (_button is null && !TryGetComponent(out _button))
2025-04-28 19:45:45 +08:00
{
2025-07-11 21:00:00 +08:00
Log.Warning("找不到Button组件");
return;
2025-04-28 19:45:45 +08:00
}
2025-07-11 21:00:00 +08:00
_button.onClick.RemoveAllListeners();
_button.onClick.AddListener(() => action?.Invoke(data));
2025-04-28 19:45:45 +08:00
}
protected internal void BindChoiceState(bool state)
{
if (ChoiseState != state)
{
ChoiseState = state;
OnBindChoiceState(state);
}
}
protected internal virtual void OnBindChoiceState(bool state)
{
}
}
}