-
Notifications
You must be signed in to change notification settings - Fork 1.9k
[iOS] Fix for Incorrect Scroll in Loop Mode When CurrentItem Is Not Found in ItemsSource #32141
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
095e82a
6420588
6a83a52
c8fb86c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -459,6 +459,12 @@ internal void UpdateLoop() | |
|
|
||
| void ScrollToPosition(int goToPosition, int carouselPosition, bool animate, bool forceScroll = false) | ||
| { | ||
| if (goToPosition < 0) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In addition to skipping negative indices, guard against |
||
| { | ||
| System.Diagnostics.Debug.WriteLine($"CarouselViewController.ScrollToPosition: Skipping scroll due to invalid goToPosition ({goToPosition})"); | ||
| return; | ||
| } | ||
|
|
||
| if (ItemsView is not CarouselView carousel) | ||
| { | ||
| return; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -387,6 +387,12 @@ internal void UpdateLoop() | |
|
|
||
| void ScrollToPosition(int goToPosition, int carouselPosition, bool animate, bool forceScroll = false) | ||
| { | ||
| if (goToPosition < 0) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mirror the same guards in CarouselViewController2 to maintain parity |
||
| { | ||
| System.Diagnostics.Debug.WriteLine($"CarouselViewController2.ScrollToPosition: Skipping scroll due to invalid goToPosition ({goToPosition})."); | ||
| return; | ||
| } | ||
|
|
||
| if (ItemsView is not CarouselView carousel) | ||
| { | ||
| return; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| using System.Collections.ObjectModel; | ||
|
|
||
| namespace Maui.Controls.Sample.Issues; | ||
|
|
||
| [Issue(IssueTracker.Github, 32139, "CarouselView scrolls to last item when Loop is enabled and CurrentItem is set to an item not in the list", PlatformAffected.iOS)] | ||
| public class Issue32139 : ContentPage | ||
| { | ||
| const string InitialItem = "0"; | ||
| const string InvalidItem = "14"; | ||
|
|
||
| Label selectedItemLabel; | ||
| CarouselView carouselView; | ||
| public Issue32139() | ||
| { | ||
| carouselView = new CarouselView | ||
| { | ||
| HeightRequest = 150, | ||
| Loop = true, | ||
| ItemsSource = new ObservableCollection<string> { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11" }, | ||
| ItemTemplate = new DataTemplate(() => | ||
| { | ||
| Border border = new Border | ||
| { | ||
| BackgroundColor = Colors.LightGray, | ||
| Content = new Label | ||
| { | ||
| VerticalOptions = LayoutOptions.Center, | ||
| HorizontalOptions = LayoutOptions.Center, | ||
| FontSize = 80 | ||
| } | ||
| }; | ||
| border.Content.SetBinding(Label.TextProperty, "."); | ||
| return border; | ||
| }) | ||
| }; | ||
|
|
||
| selectedItemLabel = new Label | ||
| { | ||
| Text = $"Selected Item : {InitialItem}", | ||
| FontSize = 16 | ||
| }; | ||
|
|
||
| Button selectButton = new Button | ||
| { | ||
| AutomationId = "Issue32139SelectBtn", | ||
| Text = $"Select item {InvalidItem}", | ||
| FontSize = 12 | ||
| }; | ||
|
|
||
| selectButton.Clicked += (s, e) => | ||
| { | ||
| carouselView.CurrentItem = InvalidItem; | ||
| selectedItemLabel.Text = $"Selected Item : {carouselView.CurrentItem}"; | ||
| }; | ||
|
|
||
| Content = new VerticalStackLayout | ||
| { | ||
| Padding = 25, | ||
| Spacing = 15, | ||
| Children = | ||
| { | ||
| carouselView, | ||
| selectedItemLabel, | ||
| selectButton, | ||
| } | ||
| }; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| #if TEST_FAILS_ON_WINDOWS && TEST_FAILS_ON_ANDROID // Related issues have been reported for both Android and Windows : Android (https://github.com/dotnet/maui/issues/29529) and Windows (https://github.com/dotnet/maui/issues/31670) | ||
| using NUnit.Framework; | ||
| using UITest.Appium; | ||
| using UITest.Core; | ||
|
|
||
| namespace Microsoft.Maui.TestCases.Tests.Issues; | ||
|
|
||
| public class Issue32139 : _IssuesUITest | ||
| { | ||
| public Issue32139(TestDevice device) : base(device) | ||
| { | ||
| } | ||
|
|
||
| public override string Issue => "CarouselView scrolls to last item when Loop is enabled and CurrentItem is set to an item not in the list"; | ||
|
|
||
| [Test] | ||
| [Category(UITestCategories.CarouselView)] | ||
| public void ValidateNoScrollOnInvalidItemWithLoop() | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can add another test? After attempting an invalid CurrentItem selection, rotate the device and verify the same item remains visible to catch layout recomputation paths that might re-trigger a scroll. |
||
| { | ||
| App.WaitForElement("Issue32139SelectBtn"); | ||
| App.Tap("Issue32139SelectBtn"); | ||
|
|
||
| VerifyScreenshot(); | ||
| } | ||
| } | ||
| #endif | ||
Uh oh!
There was an error while loading. Please reload this page.