com.alicizax.unity.ui.exten.../Runtime/RecyclerView/Layout/AlignableLinearLayoutManager.cs

49 lines
1.5 KiB
C#
Raw Normal View History

2025-06-03 13:24:23 +08:00
using System;
2025-05-30 19:40:12 +08:00
using UnityEngine;
namespace AlicizaX.UI.RecyclerView
{
2025-06-03 13:24:23 +08:00
[Serializable]
2025-05-30 19:40:12 +08:00
public class AlignableLinearLayoutManager : LinearLayoutManager
{
2025-06-03 13:24:23 +08:00
[SerializeField] private float alignmentCount = 0f; // 对齐比例 (0=顶部, 1=底部, 0.5=居中)
2025-05-30 19:40:12 +08:00
public override Vector2 CalculatePosition(int index)
{
float position;
if (direction == Direction.Vertical)
{
2025-06-03 13:24:23 +08:00
position = index * (lineHeight + spacing.y) - ScrollPosition ;
2025-05-30 19:40:12 +08:00
return new Vector2(0, position + padding.y);
}
2025-06-03 13:24:23 +08:00
position = index * (lineHeight + spacing.x) - ScrollPosition ;
2025-05-30 19:40:12 +08:00
var a = new Vector2(position + padding.x, 0);
return a;
}
public override float IndexToPosition(int index)
{
if (index < 0 || index >= adapter.GetItemCount()) return 0;
float len, viewLength, position;
if (direction == Direction.Vertical)
{
2025-06-03 13:24:23 +08:00
len = index * (lineHeight + spacing.y) - ((lineHeight + spacing.y) * alignmentCount);
2025-05-30 19:40:12 +08:00
viewLength = viewportSize.y;
position = len + viewLength > contentSize.y ? contentSize.y - viewportSize.y : len;
}
else
{
2025-06-03 13:24:23 +08:00
len = index * (lineHeight + spacing.x);
2025-05-30 19:40:12 +08:00
viewLength = viewportSize.x;
position = len + viewLength > contentSize.x ? contentSize.x - viewportSize.x : len;
}
return position;
}
}
}