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

59 lines
1.4 KiB
C#
Raw Normal View History

2025-03-12 20:59:12 +08:00
using System;
using UnityEngine;
namespace AlicizaX.UI
2025-03-12 20:59:12 +08:00
{
public abstract class ViewHolder : MonoBehaviour
{
private RectTransform rectTransform;
internal event Action<ViewHolder> Destroyed;
2025-03-28 13:34:33 +08:00
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-05-30 13:43:08 +08:00
public string Name { get; internal set; }
2026-03-11 14:18:07 +08:00
2025-05-30 13:43:08 +08:00
public int Index { get; internal set; }
2025-03-12 20:59:12 +08:00
public int DataIndex { get; internal set; } = -1;
public RecyclerView RecyclerView { get; internal set; }
2025-05-30 13:43:08 +08:00
public uint BindingVersion { get; private set; }
2026-03-30 16:00:20 +08:00
public Vector2 SizeDelta => RectTransform.sizeDelta;
2025-03-12 20:59:12 +08:00
internal uint AdvanceBindingVersion()
{
BindingVersion = BindingVersion == uint.MaxValue ? 1u : BindingVersion + 1u;
return BindingVersion;
}
2026-03-27 14:38:26 +08:00
protected internal virtual void OnRecycled()
2025-03-28 13:34:33 +08:00
{
AdvanceBindingVersion();
Name = string.Empty;
Index = -1;
DataIndex = -1;
RecyclerView = null;
2025-03-28 13:34:33 +08:00
}
2025-04-01 15:21:02 +08:00
protected virtual void OnDestroy()
2025-04-02 14:53:08 +08:00
{
Destroyed?.Invoke(this);
Destroyed = null;
2025-04-02 14:53:08 +08:00
}
2025-03-12 20:59:12 +08:00
}
}