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