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 @@ -461,6 +461,13 @@ protected virtual int DetermineTargetPosition(ScrollToRequestEventArgs args)

private static object FindBoundItemInGroup(ScrollToRequestEventArgs args, IGroupableItemsViewSource groupItemSource)
{
// When no group index is specified (groupIndex < 0), fall back to flat index lookup.
// This handles the case where ScrollTo(index) is called without a group on a grouped CollectionView.
if (args.GroupIndex < 0)
{
Comment thread
SubhikshaSf4851 marked this conversation as resolved.
Comment thread
SubhikshaSf4851 marked this conversation as resolved.
return groupItemSource.GetItem(args.Index);
}

var group = groupItemSource.GetGroupItemsViewSource(args.GroupIndex);

Comment thread
SubhikshaSf4851 marked this conversation as resolved.
// GetItem calls AdjustIndexRequest, which subtracts 1 if we have a header (UngroupedItemsSource does not do this)
Expand Down
112 changes: 112 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue35313.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
namespace Maui.Controls.Sample.Issues;

[Issue(IssueTracker.Github, 35313, "ScrollTo(0) not working on grouped CollectionView", PlatformAffected.Android)]
public class Issue35313 : ContentPage
{
CollectionView _collectionView;
List<Issue35313ItemGroup> _groups;

public Issue35313()
{
_groups = [];
for (int g = 1; g <= 5; g++)
{
var group = new Issue35313ItemGroup { Key = $"Group {g}" };
for (int i = 1; i <= 10; i++)
group.Add(new Issue35313Item { Name = $"Group {g} — Item {i}" });
_groups.Add(group);
}

_collectionView = new CollectionView
{
AutomationId = "CollectionView",
IsGrouped = true,
ItemsSource = _groups,
GroupHeaderTemplate = new DataTemplate(() =>
{
var label = new Label
{
Padding = new Thickness(8, 4),
BackgroundColor = Colors.LightGray,
FontAttributes = FontAttributes.Bold
};
label.SetBinding(Label.TextProperty, "Key");
label.SetBinding(Label.AutomationIdProperty, "Key");
return label;
}),
ItemTemplate = new DataTemplate(() =>
{
var label = new Label { Padding = new Thickness(16, 8) };
label.SetBinding(Label.TextProperty, "Name");
label.SetBinding(Label.AutomationIdProperty, "Name");
return label;
})
};

var buttonStart = new Button
{
Text = "Start",
AutomationId = "ScrollToStartButton",
Command = new Command(() => _collectionView.ScrollTo(0))
Comment thread
SubhikshaSf4851 marked this conversation as resolved.
};

var buttonFirstItem = new Button
{
Text = "First item",
AutomationId = "ScrollToFirstItemButton",
Command = new Command(() =>
{
var firstGroup = _groups[0];
_collectionView.ScrollTo(firstGroup[0], firstGroup, ScrollToPosition.Start, animate: true);
})
};

var buttonLastItem = new Button
{
Text = "Last item",
AutomationId = "ScrollToLastItemButton",
Command = new Command(() =>
{
var lastGroup = _groups[^1];
_collectionView.ScrollTo(lastGroup[^1], lastGroup, ScrollToPosition.End, animate: true);
})
};

var buttonEnd = new Button
{
Text = "End",
AutomationId = "ScrollToEndButton",
Command = new Command(() => _collectionView.ScrollTo(54))
};

var buttonRow = new HorizontalStackLayout
{
Padding = new Thickness(8),
Spacing = 8,
Children = { buttonStart, buttonFirstItem, buttonLastItem, buttonEnd }
};

Content = new Grid
{
RowDefinitions =
{
new RowDefinition { Height = GridLength.Auto },
new RowDefinition { Height = GridLength.Star },
},
Children = { buttonRow, _collectionView }
};

Grid.SetRow(buttonRow, 0);
Grid.SetRow(_collectionView, 1);
}
}

public class Issue35313Item
{
public string Name { get; set; } = string.Empty;
}

public class Issue35313ItemGroup : List<Issue35313Item>
{
public string Key { get; set; } = string.Empty;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#if ANDROID // Open Issue For iOS, MacCatalyst and Windows : https://github.com/dotnet/maui/issues/35326
Comment thread
SubhikshaSf4851 marked this conversation as resolved.
Comment thread
SubhikshaSf4851 marked this conversation as resolved.
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.TestCases.Tests.Issues;

public class Issue35313 : _IssuesUITest
{
public Issue35313(TestDevice device) : base(device) { }

public override string Issue => "ScrollTo(0) not working on grouped CollectionView";

[Test]
[Category(UITestCategories.CollectionView)]
public void GroupedCollectionViewScrollToIndexZeroShouldScrollToStart()
{
App.WaitForElement("ScrollToEndButton");

// Scroll to the end so the top is off screen
App.Tap("ScrollToEndButton");
App.WaitForElement("Group 5 — Item 10");

// ScrollTo(0) on a grouped CollectionView — this is the regression
// Without the fix it silently does nothing on Android
App.Tap("ScrollToStartButton");

// Group 1 header should be visible after scrolling to index 0
App.WaitForElement("Group 1");
}
}
#endif
Loading