Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,8 @@ void CollectionItemsSourceChanged(object sender, System.Collections.Specialized.
UpdatePosition(carouselPosition);
}

ScrollToPosition(carouselPosition);

//If we are adding or removing the last item we need to update
//the inset that we give to items so they are centered
if (e.NewStartingIndex == count - 1 || removingLastElement)
Expand Down
66 changes: 66 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue29415.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:Maui.Controls.Sample"
x:Class="Maui.Controls.Sample.Issues.Issue29415">
<Grid Padding="10"
RowDefinitions="Auto,*,*">
<VerticalStackLayout
Grid.Row="0"
Padding="10">
<HorizontalStackLayout Spacing="10">
<RadioButton x:Name="ItemsUpdatingKeepItemsInView"
Content="KeepItemsInView"
FontSize="10"
GroupName="ItemsUpdatingScrollModeGroup"
CheckedChanged="OnItemsUpdatingScrollModeChanged"
AutomationId="ItemsUpdatingKeepItemsInView"/>
<RadioButton x:Name="ItemsUpdatingKeepLastItemInView"
Content="KeepLastItemInView"
FontSize="10"
GroupName="ItemsUpdatingScrollModeGroup"
CheckedChanged="OnItemsUpdatingScrollModeChanged"
AutomationId="ItemsUpdatingKeepLastItemInView"/>
</HorizontalStackLayout>
<HorizontalStackLayout Spacing="10"
Margin="0,5,0,0">
<RadioButton x:Name="ItemsUpdatingKeepScrollOffset"
Content="KeepScrollOffset"
FontSize="10"
GroupName="ItemsUpdatingScrollModeGroup"
CheckedChanged="OnItemsUpdatingScrollModeChanged"
AutomationId="ItemsUpdatingKeepScrollOffset"/>
<Button Text="Add"
Clicked="Button_Clicked"
Command="{Binding AddItemCommand}"
WidthRequest="120"
AutomationId="AddButton"/>
</HorizontalStackLayout>
</VerticalStackLayout>
<local:CarouselView2 Grid.Row="1"
x:Name="carouselView"
HorizontalScrollBarVisibility="Never"
IndicatorView="{x:Reference indicatorView}">
<local:CarouselView2.ItemTemplate>
<DataTemplate>
<Border>
<Label Text="{Binding .}"
AutomationId="{Binding .}"
HorizontalTextAlignment="Center"
VerticalOptions="Center"/>
</Border>
</DataTemplate>
</local:CarouselView2.ItemTemplate>
</local:CarouselView2>

<IndicatorView
x:Name="indicatorView"
Grid.Row="2"
HorizontalOptions="Center"
VerticalOptions="End"
Margin="0,0,0,20"
IndicatorColor="LightGray"
SelectedIndicatorColor="DarkGray"
IndicatorSize="12"/>
</Grid>
</ContentPage>
45 changes: 45 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue29415.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System.Collections.ObjectModel;

namespace Maui.Controls.Sample.Issues;

[Issue(IssueTracker.Github, 29415, "ItemsUpdatingScrollMode in CarouselView Not Working as expected", PlatformAffected.Android)]
public partial class Issue29415 : ContentPage
{
public ObservableCollection<string> Items { get; set; }
public Issue29415()
{
InitializeComponent();
Items = new ObservableCollection<string>
{
"Item1",
"Item2",
};
BindingContext = this;
carouselView.ItemsSource = Items;
}

private void OnItemsUpdatingScrollModeChanged(object sender, CheckedChangedEventArgs e)
{
if (e.Value && sender is RadioButton radioButton)
{

switch (radioButton.Content.ToString())
{
case "KeepItemsInView":
carouselView.ItemsUpdatingScrollMode = ItemsUpdatingScrollMode.KeepItemsInView;
break;
case "KeepLastItemInView":
carouselView.ItemsUpdatingScrollMode = ItemsUpdatingScrollMode.KeepLastItemInView;
break;
case "KeepScrollOffset":
carouselView.ItemsUpdatingScrollMode = ItemsUpdatingScrollMode.KeepScrollOffset;
break;
}
}
}

private void Button_Clicked(object sender, EventArgs e)
{
Items.Add($"Item{Items.Count + 1}");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// On iOS this test fails with CollectionView, but passes with CollectionView2, so only run it with CollectionView2
#if TEST_FAILS_ON_WINDOWS // Carousel view tests fail on Windows
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.TestCases.Tests.Issues;

public class Issue29415 : _IssuesUITest
{
public Issue29415(TestDevice testDevice) : base(testDevice) { }

public override string Issue => "ItemsUpdatingScrollMode in CarouselView Not Working as expected";

[Test]
[Category(UITestCategories.CarouselView)]
public void ItemsUpdatingScrollModeShouldWork()
Comment thread
kubaflo marked this conversation as resolved.
{
App.WaitForElement("ItemsUpdatingKeepItemsInView");
App.Tap("AddButton");
App.Tap("ItemsUpdatingKeepItemsInView");
App.WaitForElement("Item1");
App.Tap("ItemsUpdatingKeepLastItemInView");
App.Tap("AddButton");
App.WaitForElement("Item4");
App.Tap("ItemsUpdatingKeepLastItemInView");
App.Tap("AddButton");
App.WaitForElement("Item5");
App.Tap("ItemsUpdatingKeepScrollOffset");
App.Tap("AddButton");
App.WaitForElement("Item5");
App.Tap("ItemsUpdatingKeepItemsInView");
App.Tap("AddButton");
App.WaitForElement("Item1");
}
}
#endif
Loading