From 255892dd5f0f4cc3c88f77bc75807b5ab1e23b35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E6=80=9D=E6=B5=B7?= <1464576565@qq.com> Date: Wed, 29 Apr 2026 19:03:59 +0800 Subject: [PATCH] =?UTF-8?q?[Opt]=20=E7=BB=9F=E4=B8=80ScrollBar=E6=98=BE?= =?UTF-8?q?=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Editor/RecyclerView/RecyclerViewEditor.cs | 38 +++++++++----------- Runtime/RecyclerView/Layout/LayoutManager.cs | 7 ++++ Runtime/RecyclerView/RecyclerView.cs | 27 +++++++++----- 3 files changed, 41 insertions(+), 31 deletions(-) diff --git a/Editor/RecyclerView/RecyclerViewEditor.cs b/Editor/RecyclerView/RecyclerViewEditor.cs index eee8bdd..fcd07a0 100644 --- a/Editor/RecyclerView/RecyclerViewEditor.cs +++ b/Editor/RecyclerView/RecyclerViewEditor.cs @@ -56,8 +56,7 @@ namespace AlicizaX.UI.Editor #region Serialized Properties - Templates & Scrollbar private SerializedProperty _templates; - private SerializedProperty _showScrollBar; - private SerializedProperty _showScrollBarOnlyWhenScrollable; + private SerializedProperty _scrollbarVisibility; private SerializedProperty _scrollbar; #endregion @@ -110,8 +109,7 @@ namespace AlicizaX.UI.Editor private void InitializeTemplateProperties() { _templates = serializedObject.FindProperty("templates"); - _showScrollBar = serializedObject.FindProperty("showScrollBar"); - _showScrollBarOnlyWhenScrollable = serializedObject.FindProperty("showScrollBarOnlyWhenScrollable"); + _scrollbarVisibility = serializedObject.FindProperty("scrollbarVisibility"); _scrollbar = serializedObject.FindProperty("scrollbar"); } @@ -354,19 +352,13 @@ namespace AlicizaX.UI.Editor { using (new EditorGUI.DisabledScope(isPlaying)) { - bool previousShowScrollBarValue = _showScrollBar.boolValue; - EditorGUILayout.PropertyField(_showScrollBar); + ScrollbarVisibility previousVisibility = (ScrollbarVisibility)_scrollbarVisibility.enumValueIndex; + EditorGUILayout.PropertyField(_scrollbarVisibility, new GUIContent("Scrollbar Visibility")); + ScrollbarVisibility currentVisibility = (ScrollbarVisibility)_scrollbarVisibility.enumValueIndex; - if (_showScrollBar.boolValue != previousShowScrollBarValue) + if (currentVisibility != previousVisibility) { - HandleScrollBarToggle(); - } - - if (_showScrollBar.boolValue) - { - EditorGUILayout.PropertyField( - _showScrollBarOnlyWhenScrollable, - new GUIContent("Only When Scrollable", "Hide the scrollbar and disable list scrolling when content does not overflow the viewport.")); + HandleScrollBarVisibilityChange(previousVisibility, currentVisibility); } } } @@ -535,22 +527,24 @@ namespace AlicizaX.UI.Editor #region Scrollbar Handling - private void HandleScrollBarToggle() + private void HandleScrollBarVisibilityChange(ScrollbarVisibility previousVisibility, ScrollbarVisibility currentVisibility) { - if (_showScrollBar.boolValue) - { - CreateScrollBar(); - } - else + if (currentVisibility == ScrollbarVisibility.AlwaysHide) { ClearScrollBar(); } + + if (previousVisibility == ScrollbarVisibility.AlwaysHide) + { + CreateScrollBar(); + } } private void CreateScrollBar() { var recyclerView = target as RecyclerView; if (recyclerView == null) return; + if (_scrollbar.objectReferenceValue != null) return; Direction direction = (Direction)_direction.enumValueIndex; string prefabPath = GetScrollbarPrefabPath(direction); @@ -589,7 +583,7 @@ namespace AlicizaX.UI.Editor private void ClearScrollBar() { - _showScrollBar.boolValue = false; + _scrollbarVisibility.enumValueIndex = (int)ScrollbarVisibility.AlwaysHide; if (_scrollbar.objectReferenceValue != null) { diff --git a/Runtime/RecyclerView/Layout/LayoutManager.cs b/Runtime/RecyclerView/Layout/LayoutManager.cs index 8f5d848..b71246a 100644 --- a/Runtime/RecyclerView/Layout/LayoutManager.cs +++ b/Runtime/RecyclerView/Layout/LayoutManager.cs @@ -229,4 +229,11 @@ namespace AlicizaX.UI Center, Top } + + public enum ScrollbarVisibility + { + AlwaysHide = 0, + AlwaysShow = 1, + WhenScrollable = 2 + } } diff --git a/Runtime/RecyclerView/RecyclerView.cs b/Runtime/RecyclerView/RecyclerView.cs index fe2b2f7..2ddd103 100644 --- a/Runtime/RecyclerView/RecyclerView.cs +++ b/Runtime/RecyclerView/RecyclerView.cs @@ -87,12 +87,11 @@ namespace AlicizaX.UI /// /// 是否显示滚动条�? /// - [HideInInspector] [SerializeField] private bool showScrollBar; + [HideInInspector] [SerializeField] private ScrollbarVisibility scrollbarVisibility; /// /// 是否仅在内容可滚动时显示滚动条�? /// - [HideInInspector] [SerializeField] private bool showScrollBarOnlyWhenScrollable; /// /// 与当前列表关联的滚动条组件�? @@ -302,13 +301,13 @@ namespace AlicizaX.UI /// /// 获取或设置是否仅在内容可滚动时显示滚动条�? /// - public bool ShowScrollBarOnlyWhenScrollable + public ScrollbarVisibility ScrollbarVisibility { - get => showScrollBarOnlyWhenScrollable; + get => scrollbarVisibility; set { - if (showScrollBarOnlyWhenScrollable == value) return; - showScrollBarOnlyWhenScrollable = value; + if (scrollbarVisibility == value) return; + scrollbarVisibility = value; RequestLayout(); } } @@ -688,7 +687,7 @@ namespace AlicizaX.UI /// private void ConfigureScrollbar() { - if (!showScrollBar || scrollbar == null) return; + if (scrollbarVisibility == ScrollbarVisibility.AlwaysHide || scrollbar == null) return; scrollbar.onValueChanged.AddListener(OnScrollbarChanged); @@ -1428,7 +1427,17 @@ namespace AlicizaX.UI /// 应显示滚动条时返�?;否则返�?�?/returns> private bool ShouldShowScrollbar() { - if (!showScrollBar || !scroll || scrollbar == null || scroller == null || layoutManager == null || !SupportsOverflowCheck()) + if (scrollbarVisibility == ScrollbarVisibility.AlwaysHide || !scroll || scrollbar == null || scroller == null || layoutManager == null) + { + return false; + } + + if (scrollbarVisibility == ScrollbarVisibility.AlwaysShow) + { + return true; + } + + if (!SupportsOverflowCheck()) { return false; } @@ -1533,7 +1542,7 @@ namespace AlicizaX.UI /// 需要仅在内容溢出时启用滚动返回 ;否则返�?�?/returns> private bool ShouldLimitScrollToOverflow() { - return showScrollBar && showScrollBarOnlyWhenScrollable && SupportsOverflowCheck(); + return scrollbarVisibility == ScrollbarVisibility.WhenScrollable && SupportsOverflowCheck(); } ///