From 64dce3454cfed0d7ce131e476b723ca77ab212eb Mon Sep 17 00:00:00 2001 From: redth Date: Fri, 19 Apr 2024 21:02:54 -0400 Subject: [PATCH] Add iOS/Android support for scrollbar visibility --- .../VirtualListViewHandler.ios.maccatalyst.cs | 10 ++++++ VirtualListView/Controls/VirtualListView.cs | 18 +++++++++++ VirtualListView/IVirtualListView.cs | 4 +++ .../Android/VirtualListViewHandler.android.cs | 31 +++++++++++++++++++ VirtualListView/VirtualListViewHandler.cs | 12 +++++++ 5 files changed, 75 insertions(+) diff --git a/VirtualListView/Apple/VirtualListViewHandler.ios.maccatalyst.cs b/VirtualListView/Apple/VirtualListViewHandler.ios.maccatalyst.cs index 595d348..84e4f2c 100644 --- a/VirtualListView/Apple/VirtualListViewHandler.ios.maccatalyst.cs +++ b/VirtualListView/Apple/VirtualListViewHandler.ios.maccatalyst.cs @@ -232,6 +232,16 @@ void UpdateEmptyView() } } + void UpdateVerticalScrollbarVisibility(ScrollBarVisibility scrollBarVisibility) + { + PlatformView.ShowsVerticalScrollIndicator = scrollBarVisibility == ScrollBarVisibility.Always || scrollBarVisibility == ScrollBarVisibility.Default; + } + + void UpdateHorizontalScrollbarVisibility(ScrollBarVisibility scrollBarVisibility) + { + PlatformView.ShowsHorizontalScrollIndicator = scrollBarVisibility == ScrollBarVisibility.Always || scrollBarVisibility == ScrollBarVisibility.Default; + } + public void InvalidateData() { this.PlatformView.InvokeOnMainThread(() => { diff --git a/VirtualListView/Controls/VirtualListView.cs b/VirtualListView/Controls/VirtualListView.cs index 4eb3371..a6d0d73 100644 --- a/VirtualListView/Controls/VirtualListView.cs +++ b/VirtualListView/Controls/VirtualListView.cs @@ -77,7 +77,25 @@ public VirtualListViewItemTemplateSelector ItemTemplateSelector public static readonly BindableProperty ItemTemplateSelectorProperty = BindableProperty.Create(nameof(ItemTemplateSelector), typeof(VirtualListViewItemTemplateSelector), typeof(VirtualListView), default); + + public ScrollBarVisibility VerticalScrollbarVisibility + { + get => (ScrollBarVisibility)GetValue(VerticalScrollbarVisibilityProperty); + set => SetValue(VerticalScrollbarVisibilityProperty, value); + } + + public static readonly BindableProperty VerticalScrollbarVisibilityProperty = + BindableProperty.Create(nameof(VerticalScrollbarVisibility), typeof(ScrollBarVisibility), typeof(VirtualListView), ScrollBarVisibility.Default); + + + public ScrollBarVisibility HorizontalScrollbarVisibility + { + get => (ScrollBarVisibility)GetValue(HorizontalScrollbarVisibilityProperty); + set => SetValue(HorizontalScrollbarVisibilityProperty, value); + } + public static readonly BindableProperty HorizontalScrollbarVisibilityProperty = + BindableProperty.Create(nameof(HorizontalScrollbarVisibility), typeof(ScrollBarVisibility), typeof(VirtualListView), ScrollBarVisibility.Default); public DataTemplate SectionHeaderTemplate { diff --git a/VirtualListView/IVirtualListView.cs b/VirtualListView/IVirtualListView.cs index 0c80ea9..eea3308 100644 --- a/VirtualListView/IVirtualListView.cs +++ b/VirtualListView/IVirtualListView.cs @@ -25,6 +25,10 @@ public interface IVirtualListView : IView IList SelectedItems { get; set; } ItemPosition? SelectedItem { get; set; } + + ScrollBarVisibility VerticalScrollbarVisibility { get; set; } + + ScrollBarVisibility HorizontalScrollbarVisibility { get; set; } event EventHandler OnSelectedItemsChanged; diff --git a/VirtualListView/Platforms/Android/VirtualListViewHandler.android.cs b/VirtualListView/Platforms/Android/VirtualListViewHandler.android.cs index c40f6fc..4f4e8cc 100644 --- a/VirtualListView/Platforms/Android/VirtualListViewHandler.android.cs +++ b/VirtualListView/Platforms/Android/VirtualListViewHandler.android.cs @@ -164,4 +164,35 @@ void UpdateEmptyView() UpdateEmptyViewVisibility(); } } + + ScrollBarVisibility _defaultHorizontalScrollVisibility = ScrollBarVisibility.Default; + ScrollBarVisibility _defaultVerticalScrollVisibility = ScrollBarVisibility.Default; + + void UpdateVerticalScrollbarVisibility(ScrollBarVisibility scrollBarVisibility) + { + if (_defaultVerticalScrollVisibility == ScrollBarVisibility.Default) + _defaultVerticalScrollVisibility = + recyclerView.VerticalScrollBarEnabled ? ScrollBarVisibility.Always : ScrollBarVisibility.Never; + + var newVerticalScrollVisiblility = scrollBarVisibility; + + if (newVerticalScrollVisiblility == ScrollBarVisibility.Default) + newVerticalScrollVisiblility = _defaultVerticalScrollVisibility; + + recyclerView.VerticalScrollBarEnabled = newVerticalScrollVisiblility == ScrollBarVisibility.Always; + } + + void UpdateHorizontalScrollbarVisibility(ScrollBarVisibility scrollBarVisibility) + { + if (_defaultHorizontalScrollVisibility == ScrollBarVisibility.Default) + _defaultHorizontalScrollVisibility = + recyclerView.HorizontalScrollBarEnabled ? ScrollBarVisibility.Always : ScrollBarVisibility.Never; + + var newHorizontalScrollVisiblility = scrollBarVisibility; + + if (newHorizontalScrollVisiblility == ScrollBarVisibility.Default) + newHorizontalScrollVisiblility = _defaultHorizontalScrollVisibility; + + recyclerView.HorizontalScrollBarEnabled = newHorizontalScrollVisiblility == ScrollBarVisibility.Always; + } } \ No newline at end of file diff --git a/VirtualListView/VirtualListViewHandler.cs b/VirtualListView/VirtualListViewHandler.cs index 2e73958..06dd620 100644 --- a/VirtualListView/VirtualListViewHandler.cs +++ b/VirtualListView/VirtualListViewHandler.cs @@ -18,6 +18,8 @@ public partial class VirtualListViewHandler [nameof(IVirtualListView.IsRefreshEnabled)] = MapIsRefreshEnabled, [nameof(IVirtualListView.EmptyView)] = MapEmptyView, [nameof(IVirtualListView.SelectedItems)] = MapSelectedItems, + [nameof(IVirtualListView.VerticalScrollbarVisibility)] = MapVerticalScrollbarVisibility, + [nameof(IVirtualListView.HorizontalScrollbarVisibility)] = MapHorizontalScrollbarVisibility, }; public static CommandMapper CommandMapper = new(ViewCommandMapper) @@ -135,5 +137,15 @@ public static void MapIsFooterVisible(VirtualListViewHandler handler, IVirtualLi { handler?.InvalidateData(); } + + public static void MapVerticalScrollbarVisibility(VirtualListViewHandler handler, IVirtualListView virtualListView) + { + handler?.UpdateVerticalScrollbarVisibility(virtualListView.VerticalScrollbarVisibility); + } + + public static void MapHorizontalScrollbarVisibility(VirtualListViewHandler handler, IVirtualListView virtualListView) + { + handler?.UpdateHorizontalScrollbarVisibility(virtualListView.HorizontalScrollbarVisibility); + } } \ No newline at end of file