-
Notifications
You must be signed in to change notification settings - Fork 1.9k
[iOS] Fix incorrect FirstVisibleItemIndex reported by CollectionView.Scrolled after programmatic scroll #33719
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5c71c47
Fix the first visible item index
Shalini-Ashokan ee551cb
Added the fix in cv1
Shalini-Ashokan 1b71732
Simplified the fix
Shalini-Ashokan 04349e8
Added test cases
Shalini-Ashokan a55ae44
modified the test case
Shalini-Ashokan 858c4b0
modified the fix
Shalini-Ashokan 807f09d
Address the AI concern
Shalini-Ashokan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| using System.Collections.ObjectModel; | ||
|
|
||
| namespace Maui.Controls.Sample.Issues; | ||
|
|
||
| [Issue(IssueTracker.Github, 33614, "CollectionView Scrolled event reports incorrect FirstVisibleItemIndex after programmatic ScrollTo", PlatformAffected.iOS | PlatformAffected.macOS)] | ||
| public class Issue33614 : ContentPage | ||
| { | ||
| public ObservableCollection<string> Items { get; set; } | ||
| private Label _firstIndexLabel; | ||
| private CollectionView2 _collectionView; | ||
| public Issue33614() | ||
| { | ||
| Items = new ObservableCollection<string>(); | ||
| for (int i = 0; i <= 50; i++) | ||
| { | ||
| Items.Add($"Item_{i}"); | ||
| } | ||
|
|
||
| _firstIndexLabel = new Label | ||
| { | ||
| AutomationId = "FirstIndexLabel", | ||
| Text = "FirstVisibleItemIndex: 0", | ||
| IsVisible = false, | ||
| }; | ||
|
|
||
| var scrollToButton = new Button | ||
| { | ||
| AutomationId = "ScrollToButton", | ||
| Text = "ScrollTo Index 15", | ||
| WidthRequest = 150 | ||
| }; | ||
| scrollToButton.Clicked += OnScrollToButtonClicked; | ||
|
|
||
| _collectionView = new CollectionView2 | ||
| { | ||
| AutomationId = "TestCollectionView", | ||
| ItemsSource = Items, | ||
| HeightRequest = 600, | ||
| ItemTemplate = new DataTemplate(() => | ||
| { | ||
| var label = new Label(); | ||
| label.SetBinding(Label.TextProperty, "."); | ||
| return new Border | ||
| { | ||
| Margin = new Thickness(5), | ||
| Padding = new Thickness(10), | ||
| Stroke = Colors.Gray, | ||
| Content = label | ||
| }; | ||
| }) | ||
| }; | ||
|
|
||
| _collectionView.Scrolled += OnCollectionViewScrolled; | ||
|
|
||
| Content = new StackLayout | ||
| { | ||
| Children = | ||
| { | ||
| _firstIndexLabel, | ||
| scrollToButton, | ||
| _collectionView | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| private void OnCollectionViewScrolled(object sender, ItemsViewScrolledEventArgs e) | ||
| { | ||
| _firstIndexLabel.Text = $"FirstVisibleItemIndex: {e.FirstVisibleItemIndex}"; | ||
| _firstIndexLabel.IsVisible = true; | ||
| } | ||
|
|
||
| private void OnScrollToButtonClicked(object sender, EventArgs e) | ||
| { | ||
| _collectionView.ScrollTo(15, position: ScrollToPosition.Start, animate: true); | ||
| } | ||
| } |
22 changes: 22 additions & 0 deletions
22
src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue33614.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| using NUnit.Framework; | ||
| using UITest.Appium; | ||
| using UITest.Core; | ||
|
|
||
| namespace Microsoft.Maui.TestCases.Tests.Issues; | ||
|
|
||
| public class Issue33614 : _IssuesUITest | ||
| { | ||
| public override string Issue => "CollectionView Scrolled event reports incorrect FirstVisibleItemIndex after programmatic ScrollTo"; | ||
|
|
||
| public Issue33614(TestDevice device) : base(device) { } | ||
|
|
||
| [Test] | ||
| [Category(UITestCategories.CollectionView)] | ||
| public void FirstVisibleItemIndexShouldBeCorrectAfterScrollTo() | ||
| { | ||
| App.WaitForElement("ScrollToButton"); | ||
| App.Tap("ScrollToButton"); | ||
| var firstIndexText = App.FindElement("FirstIndexLabel").GetText(); | ||
| Assert.That(firstIndexText, Is.EqualTo("FirstVisibleItemIndex: 15")); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test may have a timing issue. After tapping the button, the test immediately reads the label text without waiting for the scroll animation to complete or for the label to update. This could lead to a race condition where the test reads the old value before the Scrolled event fires and updates the label.
Consider adding a wait or retry mechanism to ensure the label has been updated with the expected value. For example, you could use App.WaitForElement with a lambda that checks for the expected text, or add Task.Delay similar to other tests that wait for scroll animations (see Issue18961.cs line 29 which uses
await Task.Delay(1000)after a scroll).