From b9b8d349fd861d38ac2d204e0f61851b5070b5ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E6=80=9D=E6=B5=B7?= <1464576565@qq.com> Date: Tue, 3 Jun 2025 13:24:23 +0800 Subject: [PATCH] modify --- .../Layout/AlignableLinearLayoutManager.cs | 37 ++++--------------- 1 file changed, 7 insertions(+), 30 deletions(-) diff --git a/Runtime/RecyclerView/Layout/AlignableLinearLayoutManager.cs b/Runtime/RecyclerView/Layout/AlignableLinearLayoutManager.cs index bd7fc35..457dafa 100644 --- a/Runtime/RecyclerView/Layout/AlignableLinearLayoutManager.cs +++ b/Runtime/RecyclerView/Layout/AlignableLinearLayoutManager.cs @@ -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; } }