Skip to content

Commit f3a4110

Browse files
committed
[Android] ItemsUpdatingScrollMode in CarouselView
1 parent 5030975 commit f3a4110

File tree

4 files changed

+151
-0
lines changed

4 files changed

+151
-0
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,9 @@ void CollectionItemsSourceChanged(object sender, System.Collections.Specialized.
303303
UpdatePosition(carouselPosition);
304304
}
305305

306+
UpdateAdapter();
307+
ScrollToPosition(carouselPosition);
308+
306309
//If we are adding or removing the last item we need to update
307310
//the inset that we give to items so they are centered
308311
if (e.NewStartingIndex == count - 1 || removingLastElement)
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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+
xmlns:local="clr-namespace:Maui.Controls.Sample"
5+
x:Class="Maui.Controls.Sample.Issues.Issue29415">
6+
<Grid Padding="10"
7+
RowDefinitions="Auto,*,*">
8+
<VerticalStackLayout
9+
Grid.Row="0"
10+
Padding="10">
11+
<HorizontalStackLayout Spacing="10">
12+
<RadioButton x:Name="ItemsUpdatingKeepItemsInView"
13+
Content="KeepItemsInView"
14+
FontSize="10"
15+
GroupName="ItemsUpdatingScrollModeGroup"
16+
CheckedChanged="OnItemsUpdatingScrollModeChanged"
17+
AutomationId="ItemsUpdatingKeepItemsInView"/>
18+
<RadioButton x:Name="ItemsUpdatingKeepLastItemInView"
19+
Content="KeepLastItemInView"
20+
FontSize="10"
21+
GroupName="ItemsUpdatingScrollModeGroup"
22+
CheckedChanged="OnItemsUpdatingScrollModeChanged"
23+
AutomationId="ItemsUpdatingKeepLastItemInView"/>
24+
</HorizontalStackLayout>
25+
<HorizontalStackLayout Spacing="10"
26+
Margin="0,5,0,0">
27+
<RadioButton x:Name="ItemsUpdatingKeepScrollOffset"
28+
Content="KeepScrollOffset"
29+
FontSize="10"
30+
GroupName="ItemsUpdatingScrollModeGroup"
31+
CheckedChanged="OnItemsUpdatingScrollModeChanged"
32+
AutomationId="ItemsUpdatingKeepScrollOffset"/>
33+
<Button Text="Add"
34+
Clicked="Button_Clicked"
35+
Command="{Binding AddItemCommand}"
36+
WidthRequest="120"
37+
AutomationId="AddButton"/>
38+
</HorizontalStackLayout>
39+
</VerticalStackLayout>
40+
<local:CarouselView2 Grid.Row="1"
41+
x:Name="carouselView"
42+
HorizontalScrollBarVisibility="Never"
43+
IndicatorView="{x:Reference indicatorView}">
44+
<local:CarouselView2.ItemTemplate>
45+
<DataTemplate>
46+
<Border>
47+
<Label Text="{Binding .}"
48+
AutomationId="{Binding .}"
49+
HorizontalTextAlignment="Center"
50+
VerticalOptions="Center"/>
51+
</Border>
52+
</DataTemplate>
53+
</local:CarouselView2.ItemTemplate>
54+
</local:CarouselView2>
55+
56+
<IndicatorView
57+
x:Name="indicatorView"
58+
Grid.Row="2"
59+
HorizontalOptions="Center"
60+
VerticalOptions="End"
61+
Margin="0,0,0,20"
62+
IndicatorColor="LightGray"
63+
SelectedIndicatorColor="DarkGray"
64+
IndicatorSize="12"/>
65+
</Grid>
66+
</ContentPage>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using System.Collections.ObjectModel;
2+
3+
namespace Maui.Controls.Sample.Issues;
4+
5+
[Issue(IssueTracker.Github, 29415, "ItemsUpdatingScrollMode in CarouselView Not Working as expected", PlatformAffected.Android)]
6+
public partial class Issue29415 : ContentPage
7+
{
8+
public ObservableCollection<string> Items { get; set; }
9+
public Issue29415()
10+
{
11+
InitializeComponent();
12+
Items = new ObservableCollection<string>
13+
{
14+
"Item1",
15+
"Item2",
16+
};
17+
BindingContext = this;
18+
carouselView.ItemsSource = Items;
19+
}
20+
21+
private void OnItemsUpdatingScrollModeChanged(object sender, CheckedChangedEventArgs e)
22+
{
23+
if (e.Value && sender is RadioButton radioButton)
24+
{
25+
26+
switch (radioButton.Content.ToString())
27+
{
28+
case "KeepItemsInView":
29+
carouselView.ItemsUpdatingScrollMode = ItemsUpdatingScrollMode.KeepItemsInView;
30+
break;
31+
case "KeepLastItemInView":
32+
carouselView.ItemsUpdatingScrollMode = ItemsUpdatingScrollMode.KeepLastItemInView;
33+
break;
34+
case "KeepScrollOffset":
35+
carouselView.ItemsUpdatingScrollMode = ItemsUpdatingScrollMode.KeepScrollOffset;
36+
break;
37+
}
38+
}
39+
}
40+
41+
private void Button_Clicked(object sender, EventArgs e)
42+
{
43+
Items.Add($"Item{Items.Count + 1}");
44+
}
45+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// On iOS this test fails with CollectionView, but passes with CollectionView2, so only run it with CollectionView2
2+
#if TEST_FAILS_ON_WINDOWS // Carousel view tests fail on Windows
3+
using NUnit.Framework;
4+
using UITest.Appium;
5+
using UITest.Core;
6+
7+
namespace Microsoft.Maui.TestCases.Tests.Issues;
8+
9+
public class Issue29415 : _IssuesUITest
10+
{
11+
public Issue29415(TestDevice testDevice) : base(testDevice) { }
12+
13+
public override string Issue => "ItemsUpdatingScrollMode in CarouselView Not Working as expected";
14+
15+
[Test]
16+
[Category(UITestCategories.CarouselView)]
17+
public void ItemsUpdatingScrollModeShouldWork()
18+
{
19+
App.WaitForElement("ItemsUpdatingKeepItemsInView");
20+
App.Tap("AddButton");
21+
App.Tap("ItemsUpdatingKeepItemsInView");
22+
App.WaitForElement("Item1");
23+
App.Tap("ItemsUpdatingKeepLastItemInView");
24+
App.Tap("AddButton");
25+
App.WaitForElement("Item4");
26+
App.Tap("ItemsUpdatingKeepLastItemInView");
27+
App.Tap("AddButton");
28+
App.WaitForElement("Item5");
29+
App.Tap("ItemsUpdatingKeepScrollOffset");
30+
App.Tap("AddButton");
31+
App.WaitForElement("Item5");
32+
App.Tap("ItemsUpdatingKeepItemsInView");
33+
App.Tap("AddButton");
34+
App.WaitForElement("Item1");
35+
}
36+
}
37+
#endif

0 commit comments

Comments
 (0)