Skip to content

[Android] Fix CollectionView selection crash with HeaderTemplate#34275

Open
NirmalKumarYuvaraj wants to merge 1 commit intodotnet:mainfrom
NirmalKumarYuvaraj:fix-34247
Open

[Android] Fix CollectionView selection crash with HeaderTemplate#34275
NirmalKumarYuvaraj wants to merge 1 commit intodotnet:mainfrom
NirmalKumarYuvaraj:fix-34247

Conversation

@NirmalKumarYuvaraj
Copy link
Contributor

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 CollectionView with a HeaderTemplate and SelectionMode.Single on Android. The fix ensures that header and footer view holders do not participate in selection tracking, preventing an ArgumentOutOfRangeException. 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:

  • Updated SelectableItemsViewAdapter.cs to skip selection tracking for header and footer view holders, preventing crashes when selecting items in a CollectionView with headers or footers.

Test coverage improvements:

  • Added a new page (Issue34247.cs) to the sample app to reproduce and verify the selection bug in a CollectionView with a header template and single selection mode.
  • Introduced an automated UI test (Issue34247.cs) to ensure that selecting an item in the affected CollectionView does not cause a crash and updates the result label as expected.

Issues Fixed

Fixes #34247

@github-actions
Copy link
Contributor

🚀 Dogfood this PR with:

⚠️ WARNING: Do not do this without first carefully reviewing the code of this PR to satisfy yourself it is safe.

curl -fsSL https://raw.githubusercontent.com/dotnet/maui/main/eng/scripts/get-maui-pr.sh | bash -s -- 34275

Or

  • Run remotely in PowerShell:
iex "& { $(irm https://raw.githubusercontent.com/dotnet/maui/main/eng/scripts/get-maui-pr.ps1) } 34275"

@dotnet-policy-service dotnet-policy-service bot added the partner/syncfusion Issues / PR's with Syncfusion collaboration label Feb 27, 2026
@NirmalKumarYuvaraj NirmalKumarYuvaraj added community ✨ Community Contribution area-controls-collectionview CollectionView, CarouselView, IndicatorView platform/android labels Feb 27, 2026
@sheiksyedm sheiksyedm marked this pull request as ready for review February 27, 2026 14:56
Copilot AI review requested due to automatic review settings February 27, 2026 14:56
@sheiksyedm sheiksyedm added this to the .NET 10 SR6 milestone Feb 27, 2026
@sheiksyedm
Copy link
Contributor

/azp run maui-pr-uitests

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.OnBindViewHolder to prevent header/footer holders from being added to selection tracking.
  • Add Issue34247 HostApp page reproducing single-selection with a HeaderTemplate.
  • Add Issue34247 Appium 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.

Comment on lines +19 to +23
App.WaitForElement("TestCollectionView");
App.WaitForElement("Item 1");
App.Tap("Item 1");
var result = App.WaitForElement("ResultLabel").GetText();
Assert.That(result, Is.EqualTo("Success"));
Copy link

Copilot AI Feb 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.

namespace Maui.Controls.Sample.Issues;

[Issue(IssueTracker.Github, 34247, "CollectionView with HeaderTemplate and SelectionMode.Single crashes on selection", PlatformAffected.All)]
Copy link

Copilot AI Feb 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
[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)]

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-controls-collectionview CollectionView, CarouselView, IndicatorView community ✨ Community Contribution partner/syncfusion Issues / PR's with Syncfusion collaboration platform/android

Projects

None yet

4 participants