Skip to content

Commit 06cd04d

Browse files
authored
[Android] Added a scroll bar for collection view (#25471)
1 parent 19dcdcd commit 06cd04d

File tree

7 files changed

+69
-6
lines changed

7 files changed

+69
-6
lines changed

src/Controls/src/Core/Handlers/Items/Android/MauiRecyclerView.cs

+2-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.ComponentModel;
55
using System.Text;
66
using Android.Content;
7+
using Android.Views;
78
using AndroidX.RecyclerView.Widget;
89
using Microsoft.Maui.Controls.Internals;
910
using Microsoft.Maui.Controls.Platform;
@@ -47,16 +48,13 @@ public class MauiRecyclerView<TItemsView, TAdapter, TItemsViewSource> : Recycler
4748

4849
~MauiRecyclerView() => _layoutPropertyChangedProxy?.Unsubscribe();
4950

50-
public MauiRecyclerView(Context context, Func<IItemsLayout> getItemsLayout, Func<TAdapter> getAdapter) : base(context)
51+
public MauiRecyclerView(Context context, Func<IItemsLayout> getItemsLayout, Func<TAdapter> getAdapter) : base(new ContextThemeWrapper(context, Resource.Style.collectionViewTheme))
5152
{
5253
_getItemsLayout = getItemsLayout ?? throw new ArgumentNullException(nameof(getItemsLayout));
5354
CreateAdapter = getAdapter ?? throw new ArgumentNullException(nameof(getAdapter));
5455

5556
_emptyCollectionObserver = new DataChangeObserver(UpdateEmptyViewVisibility);
5657
_itemsUpdateScrollObserver = new DataChangeObserver(AdjustScrollForItemUpdate);
57-
58-
VerticalScrollBarEnabled = false;
59-
HorizontalScrollBarEnabled = false;
6058
}
6159

6260
public virtual void TearDownOldElement(TItemsView oldElement)

src/Controls/src/Core/Handlers/Items/ItemsViewHandler.Android.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public static void MapHorizontalScrollBarVisibility(ItemsViewHandler<TItemsView>
7171

7272
public static void MapVerticalScrollBarVisibility(ItemsViewHandler<TItemsView> handler, ItemsView itemsView)
7373
{
74-
(handler.PlatformView as IMauiRecyclerView<TItemsView>)?.UpdateHorizontalScrollBarVisibility();
74+
(handler.PlatformView as IMauiRecyclerView<TItemsView>)?.UpdateVerticalScrollBarVisibility();
7575
}
7676

7777
public static void MapItemTemplate(ItemsViewHandler<TItemsView> handler, ItemsView itemsView)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
4+
x:Class="Maui.Controls.Sample.Issues.Issue25468">
5+
<CollectionView
6+
VerticalScrollBarVisibility="Always"
7+
x:Name="CollectionView">
8+
<CollectionView.ItemTemplate>
9+
<DataTemplate>
10+
<Label Text="{Binding}"
11+
AutomationId="{Binding}"/>
12+
</DataTemplate>
13+
</CollectionView.ItemTemplate>
14+
</CollectionView>
15+
</ContentPage>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
namespace Maui.Controls.Sample.Issues
2+
{
3+
[Issue(IssueTracker.Github, 25468, "Collection view has no scroll bar", PlatformAffected.Android)]
4+
public partial class Issue25468 : ContentPage
5+
{
6+
public Issue25468()
7+
{
8+
InitializeComponent();
9+
10+
// Modifying the scroll bar so that we can verify screenshot
11+
#if ANDROID
12+
Microsoft.Maui.Controls.Handlers.Items.CollectionViewHandler.Mapper.AppendToMapping("CollectionViewHandler", (handler, view) =>
13+
{
14+
handler.PlatformView.ScrollbarFadingEnabled = false;
15+
handler.PlatformView.ScrollBarFadeDuration = 20;
16+
handler.PlatformView.ScrollBarSize = 50;
17+
handler.PlatformView.ScrollBarStyle = Android.Views.ScrollbarStyles.OutsideInset;
18+
});
19+
#endif
20+
21+
var items = Enumerable.Range(1, 100).Select(i => i.ToString()).ToList();
22+
CollectionView.ItemsSource = items;
23+
}
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#if ANDROID
2+
using NUnit.Framework;
3+
using UITest.Appium;
4+
using UITest.Core;
5+
6+
namespace Microsoft.Maui.TestCases.Tests.Issues
7+
{
8+
public class Issue25468 : _IssuesUITest
9+
{
10+
public Issue25468(TestDevice testDevice) : base(testDevice)
11+
{
12+
}
13+
14+
public override string Issue => "Collection view has no scroll bar";
15+
16+
[Test]
17+
[Category(UITestCategories.CollectionView)]
18+
public void CollectionViewShouldHaveScrollBar()
19+
{
20+
App.WaitForElement("1");
21+
VerifyScreenshot();
22+
}
23+
}
24+
}
25+
#endif

src/Core/src/Platform/Android/Resources/values/styles.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@
9595
</style>
9696

9797
<style name="collectionViewTheme">
98-
<item name="collectionViewStyle">@style/scrollViewScrollBars</item>
98+
<item name="android:scrollbars">vertical|horizontal</item>
9999
</style>
100100

101101
<style name="scrollViewTheme">

0 commit comments

Comments
 (0)