This commit is contained in:
陈思海 2025-06-03 13:24:23 +08:00
parent 65f9bbe275
commit b9b8d349fd

View File

@ -1,44 +1,25 @@
using System;
using UnityEngine;
namespace AlicizaX.UI.RecyclerView
{
[Serializable]
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);
}
}
[SerializeField] private float alignmentCount = 0f; // 对齐比例 (0=顶部, 1=底部, 0.5=居中)
public override Vector2 CalculatePosition(int index)
{
float position;
float alignmentOffset = CalculateAlignmentOffset();
if (direction == Direction.Vertical)
{
position = index * (lineHeight + spacing.y) - ScrollPosition + alignmentOffset;
position = index * (lineHeight + spacing.y) - ScrollPosition ;
return new Vector2(0, position + padding.y);
}
position = index * (lineHeight + spacing.x) - ScrollPosition + alignmentOffset;
position = index * (lineHeight + spacing.x) - ScrollPosition ;
var a = new Vector2(position + padding.x, 0);
Debug.Log("Calcu" + a);
return a;
}
@ -47,24 +28,20 @@ namespace AlicizaX.UI.RecyclerView
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);
len = index * (lineHeight + spacing.y) - ((lineHeight + spacing.y) * alignmentCount);
viewLength = viewportSize.y;
position = len + viewLength > contentSize.y ? contentSize.y - viewportSize.y : len;
}
else
{
len = index * (lineHeight + spacing.x) + alignmentOffset;
len = index * (lineHeight + spacing.x);
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;
}
}