-
Notifications
You must be signed in to change notification settings - Fork 1.9k
[iOS, Mac] Fix for CollectionView not triggering Scrolled event when grouped and groups are empty #30375
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
[iOS, Mac] Fix for CollectionView not triggering Scrolled event when grouped and groups are empty #30375
Changes from all commits
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 |
|---|---|---|
|
|
@@ -30,8 +30,10 @@ public override void Scrolled(UIScrollView scrollView) | |
| { | ||
| var (visibleItems, firstVisibleItemIndex, centerItemIndex, lastVisibleItemIndex) = GetVisibleItemsIndex(); | ||
|
|
||
| if (!visibleItems) | ||
| if (!visibleItems && !HasHeaderOrFooter()) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| var contentInset = scrollView.ContentInset; | ||
| var contentOffsetX = scrollView.ContentOffset.X + contentInset.Left; | ||
|
|
@@ -148,6 +150,37 @@ protected virtual (bool VisibleItems, int First, int Center, int Last) GetVisibl | |
| return (VisibleItems, firstVisibleItemIndex, centerItemIndex, lastVisibleItemIndex); | ||
| } | ||
|
|
||
| bool HasHeaderOrFooter() | ||
|
||
| { | ||
| var viewController = ViewController; | ||
|
|
||
| if (viewController?.ItemsView is null) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| // Check for structured headers/footers (overall CollectionView header/footer) | ||
| if (viewController.ItemsView is StructuredItemsView structuredItemsView) | ||
| { | ||
| if (structuredItemsView.Header is not null || structuredItemsView.HeaderTemplate is not null || | ||
| structuredItemsView.Footer is not null || structuredItemsView.FooterTemplate is not null) | ||
| { | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| // Check for group headers/footers (grouped CollectionView) | ||
| if (viewController.ItemsView is GroupableItemsView groupableItemsView && groupableItemsView.IsGrouped) | ||
| { | ||
| if (groupableItemsView.GroupHeaderTemplate is not null || groupableItemsView.GroupFooterTemplate is not null) | ||
| { | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| static int GetItemIndex(NSIndexPath indexPath, IItemsViewSource itemSource) | ||
| { | ||
| int index = (int)indexPath.Item; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| using System.Collections.ObjectModel; | ||
|
|
||
| namespace Controls.TestCases.HostApp.Issues; | ||
|
|
||
| [Issue(IssueTracker.Github, 30249, "Grouped CollectionView does not trigger the Scrolled event for empty groups", PlatformAffected.iOS)] | ||
| public class Issue30249 : ContentPage | ||
| { | ||
| CollectionView collectionViewWithEmptyGroups; | ||
| ObservableCollection<Issue30249Group> groupedItems = new ObservableCollection<Issue30249Group>(); | ||
| Label scrolledEventStatusLabel; | ||
|
|
||
| public Issue30249() | ||
| { | ||
| Label descriptionLabel = new Label | ||
| { | ||
| Text = "Verify CollectionView Scrolled event is triggered for empty groups", | ||
| Margin = new Thickness(10) | ||
| }; | ||
|
|
||
| scrolledEventStatusLabel = new Label | ||
| { | ||
| AutomationId = "ScrolledEventStatusLabel", | ||
|
||
| Text = "Failure", | ||
| Margin = new Thickness(10) | ||
| }; | ||
|
|
||
| collectionViewWithEmptyGroups = new CollectionView | ||
| { | ||
| AutomationId = "CollectionViewWithEmptyGroups", | ||
| IsGrouped = true | ||
| }; | ||
| collectionViewWithEmptyGroups.Scrolled += CollectionView_Scrolled; | ||
|
|
||
| collectionViewWithEmptyGroups.GroupHeaderTemplate = new DataTemplate(() => | ||
| { | ||
| Label label = new Label | ||
| { | ||
| Padding = new Thickness(10), | ||
| }; | ||
|
|
||
| label.SetBinding(Label.TextProperty, "Title"); | ||
| return label; | ||
| }); | ||
|
|
||
| for (int group = 0; group < 20; group++) | ||
| { | ||
| groupedItems.Add(new Issue30249Group($"Group {group}", new List<string>())); | ||
| } | ||
|
|
||
| collectionViewWithEmptyGroups.ItemsSource = groupedItems; | ||
|
|
||
| Grid grid = new Grid | ||
| { | ||
| RowDefinitions = | ||
| { | ||
| new RowDefinition { Height = GridLength.Auto }, | ||
| new RowDefinition { Height = GridLength.Auto }, | ||
| new RowDefinition { Height = GridLength.Star } | ||
| } | ||
| }; | ||
|
|
||
| grid.Add(descriptionLabel, 0, 0); | ||
| grid.Add(scrolledEventStatusLabel, 0, 1); | ||
| grid.Add(collectionViewWithEmptyGroups, 0, 2); | ||
|
|
||
| Content = grid; | ||
| } | ||
|
|
||
| private void CollectionView_Scrolled(object sender, ItemsViewScrolledEventArgs e) | ||
| { | ||
| scrolledEventStatusLabel.Text = "Success"; | ||
| } | ||
| } | ||
|
|
||
| public class Issue30249Group : List<string> | ||
| { | ||
| public string Title { get; set; } | ||
|
|
||
| public Issue30249Group(string title, List<string> items) : base(items) | ||
| { | ||
| Title = title; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,25 @@ | ||||||||
| using NUnit.Framework; | ||||||||
| using UITest.Appium; | ||||||||
| using UITest.Core; | ||||||||
|
|
||||||||
| namespace Microsoft.Maui.TestCases.Tests.Issues; | ||||||||
|
|
||||||||
| public class Issue30249 : _IssuesUITest | ||||||||
| { | ||||||||
| public Issue30249(TestDevice device) : base(device) | ||||||||
| { | ||||||||
| } | ||||||||
|
|
||||||||
| public override string Issue => "Grouped CollectionView does not trigger the Scrolled event for empty groups"; | ||||||||
|
|
||||||||
| [Test] | ||||||||
| [Category(UITestCategories.CollectionView)] | ||||||||
| public void VerifyScrolledEventForEmptyGroups() | ||||||||
| { | ||||||||
| App.WaitForElement("CollectionViewWithEmptyGroups"); | ||||||||
| App.ScrollDown("CollectionViewWithEmptyGroups", ScrollStrategy.Gesture, 0.2, 500); | ||||||||
|
|
||||||||
|
||||||||
| App.WaitForElement("ScrolledEventStatusLabel"); |
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.
This helper method is duplicated in ItemsViewDelegator2; consider extracting it into a shared base class or utility to avoid code duplication.