com.alicizax.unity.ui.exten.../Runtime/RecyclerView/Layout/AlignableLinearLayoutManager.cs
2025-05-30 19:40:12 +08:00

72 lines
2.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using UnityEngine;
namespace AlicizaX.UI.RecyclerView
{
public class AlignableLinearLayoutManager : LinearLayoutManager
{
[SerializeField] private float alignmentRatio = 0f; // 对齐比例 (0=顶部, 1=底部, 0.5=居中)
private float CalculateAlignmentOffset()
{
if (direction == Direction.Vertical)
{
// 垂直布局:计算视口高度相关的偏移
float viewportHeight = viewportSize.y;
float itemHeight = lineHeight;
return alignmentRatio * (viewportHeight - itemHeight);
}
else
{
// 水平布局:计算视口宽度相关的偏移
float viewportWidth = viewportSize.x;
float itemWidth = lineHeight; // 注意水平布局中lineHeight表示宽度
return alignmentRatio * (viewportWidth - itemWidth);
}
}
public override Vector2 CalculatePosition(int index)
{
float position;
float alignmentOffset = CalculateAlignmentOffset();
if (direction == Direction.Vertical)
{
position = index * (lineHeight + spacing.y) - ScrollPosition + alignmentOffset;
return new Vector2(0, position + padding.y);
}
position = index * (lineHeight + spacing.x) - ScrollPosition + alignmentOffset;
var a = new Vector2(position + padding.x, 0);
Debug.Log("Calcu" + a);
return a;
}
public override float IndexToPosition(int index)
{
if (index < 0 || index >= adapter.GetItemCount()) return 0;
float len, viewLength, position;
float alignmentOffset = CalculateAlignmentOffset();
if (direction == Direction.Vertical)
{
len = index * (lineHeight + spacing.y) + alignmentOffset - ((lineHeight + spacing.y) * 2);
viewLength = viewportSize.y;
position = len + viewLength > contentSize.y ? contentSize.y - viewportSize.y : len;
}
else
{
len = index * (lineHeight + spacing.x) + alignmentOffset;
viewLength = viewportSize.x;
position = len + viewLength > contentSize.x ? contentSize.x - viewportSize.x : len;
}
Debug.Log($"index:{index} len:{len} view:{viewLength} position:{position}");
return position;
}
}
}