[Android] Fix CollectionView selection crash with HeaderTemplate#34275
[Android] Fix CollectionView selection crash with HeaderTemplate#34275NirmalKumarYuvaraj wants to merge 1 commit intodotnet:mainfrom
Conversation
|
🚀 Dogfood this PR with:
curl -fsSL https://raw.githubusercontent.com/dotnet/maui/main/eng/scripts/get-maui-pr.sh | bash -s -- 34275Or
iex "& { $(irm https://raw.githubusercontent.com/dotnet/maui/main/eng/scripts/get-maui-pr.ps1) } 34275" |
|
/azp run maui-pr-uitests |
|
Azure Pipelines successfully started running 1 pipeline(s). |
There was a problem hiding this comment.
Pull request overview
Fixes an Android crash in CollectionView selection when a HeaderTemplate/FooterTemplate is present by ensuring header/footer view holders are excluded from selection tracking, and adds a new HostApp repro page + UI test for regression coverage.
Changes:
- Skip header/footer positions in
SelectableItemsViewAdapter.OnBindViewHolderto prevent header/footer holders from being added to selection tracking. - Add
Issue34247HostApp page reproducing single-selection with aHeaderTemplate. - Add
Issue34247Appium UI test validating selection works without crashing.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/Controls/src/Core/Handlers/Items/Android/Adapters/SelectableItemsViewAdapter.cs | Prevents header/footer holders from participating in selection tracking to avoid GetItem() crashes. |
| src/Controls/tests/TestCases.HostApp/Issues/Issue34247.cs | Adds a minimal repro page with HeaderTemplate + SelectionMode.Single. |
| src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue34247.cs | Adds an automated UI test that selects an item and verifies expected UI state. |
| App.WaitForElement("TestCollectionView"); | ||
| App.WaitForElement("Item 1"); | ||
| App.Tap("Item 1"); | ||
| var result = App.WaitForElement("ResultLabel").GetText(); | ||
| Assert.That(result, Is.EqualTo("Success")); |
There was a problem hiding this comment.
The test reads ResultLabel text immediately after tapping. WaitForElement("ResultLabel") only waits for the element to exist, not for its text to update, which can make this test flaky. Consider waiting for the expected text (e.g., WaitForTextToBePresentInElement("ResultLabel", "Success") with a timeout) before asserting.
|
|
||
| namespace Maui.Controls.Sample.Issues; | ||
|
|
||
| [Issue(IssueTracker.Github, 34247, "CollectionView with HeaderTemplate and SelectionMode.Single crashes on selection", PlatformAffected.All)] |
There was a problem hiding this comment.
This issue is Android-specific (per the linked bug and the PR title), but the [Issue] attribute marks PlatformAffected.All. Please update this to PlatformAffected.Android (or the minimal set of affected platforms) so the metadata matches the actual scope of the bug.
| [Issue(IssueTracker.Github, 34247, "CollectionView with HeaderTemplate and SelectionMode.Single crashes on selection", PlatformAffected.All)] | |
| [Issue(IssueTracker.Github, 34247, "CollectionView with HeaderTemplate and SelectionMode.Single crashes on selection", PlatformAffected.Android)] |
Note
Are you waiting for the changes in this PR to be merged?
It would be very helpful if you could test the resulting artifacts from this PR and let us know in a comment if this change resolves your issue. Thank you!
Root Cause
When a CollectionView has a HeaderTemplate and SelectionMode is Single, clicking on an item threw ArgumentOutOfRangeException. The root cause was that SelectableItemsViewAdapter.OnBindViewHolder added header/footer ViewHolders to the selection tracking list. When MarkPlatformSelection iterated these holders, it called GetItem() on header positions, causing AdjustIndexForHeader(0) to return -1, which then crashed in ElementAt(-1).
Description of Change
The fix prevents header and footer view holders from entering the selection tracking system entirely by checking IsHeader/IsFooter in OnBindViewHolder before subscribing click handlers or adding to _currentViewHolders.
This pull request addresses a crash that occurred when selecting items in a
CollectionViewwith aHeaderTemplateandSelectionMode.Singleon Android. The fix ensures that header and footer view holders do not participate in selection tracking, preventing anArgumentOutOfRangeException. Additionally, new test cases are added to verify the fix both in the sample app and via automated UI testing.Bug fix for selection in CollectionView with headers/footers:
SelectableItemsViewAdapter.csto skip selection tracking for header and footer view holders, preventing crashes when selecting items in aCollectionViewwith headers or footers.Test coverage improvements:
Issue34247.cs) to the sample app to reproduce and verify the selection bug in aCollectionViewwith a header template and single selection mode.Issue34247.cs) to ensure that selecting an item in the affectedCollectionViewdoes not cause a crash and updates the result label as expected.Issues Fixed
Fixes #34247