-
Notifications
You must be signed in to change notification settings - Fork 1.9k
[Android] CollectionView: Fix SelectedItem visual state not applying when re-selecting same item #31591
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
kubaflo
merged 8 commits into
dotnet:inflight/current
from
KarthikRajaKalaimani:Fix-20062
Apr 1, 2026
Merged
[Android] CollectionView: Fix SelectedItem visual state not applying when re-selecting same item #31591
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
6154f93
Fixed the CollectionView - SelectedItem visual state manager not working
KarthikRajaKalaimani d531db9
Test case included
KarthikRajaKalaimani 5a3b7b2
Included automation Id for Tapping
KarthikRajaKalaimani afd1e82
Snapshot added
KarthikRajaKalaimani 9f174e1
Fixed modified and added snapshot for iOS.
KarthikRajaKalaimani 7f2b9cc
Snapshot updated
KarthikRajaKalaimani 142f41f
Code formatted
KarthikRajaKalaimani 00872d1
Fix improved and test case modified
KarthikRajaKalaimani 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
Binary file added
BIN
+21.2 KB
...s.Android.Tests/snapshots/android/CollectionViewSelectionChangesVisualState.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
101 changes: 101 additions & 0 deletions
101
src/Controls/tests/TestCases.HostApp/Issues/Issue20062.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,101 @@ | ||
| using Microsoft.Maui.Controls.Shapes; | ||
|
|
||
| namespace Maui.Controls.Sample.Issues; | ||
|
|
||
| [Issue(IssueTracker.Github, 20062, "CollectionView - SelectedItem visual state manager not working", | ||
| PlatformAffected.Android)] | ||
| public class Issue20062 : TestContentPage | ||
| { | ||
| CollectionView _collectionView; | ||
| List<string> list = new List<string>(); | ||
|
|
||
| protected override void Init() | ||
| { | ||
|
|
||
| _collectionView = new CollectionView | ||
| { | ||
| SelectionMode = SelectionMode.Single, | ||
| ItemsLayout = new GridItemsLayout(2, ItemsLayoutOrientation.Vertical) | ||
| { | ||
| HorizontalItemSpacing = 12, | ||
| VerticalItemSpacing = 12 | ||
| }, | ||
| ItemTemplate = new DataTemplate(() => | ||
| { | ||
| var border = new Border | ||
| { | ||
| Padding = 4, | ||
| BackgroundColor = Colors.White, | ||
| Stroke = Colors.Gray, | ||
| StrokeShape = new RoundRectangle { CornerRadius = 12 }, | ||
| StrokeThickness = 1 | ||
| }; | ||
|
|
||
| var label = new Label | ||
| { | ||
| FontFamily = "SemiBold", | ||
| TextColor = Colors.Black, | ||
| Text = "a", | ||
| FontSize = 14, | ||
| HorizontalOptions = LayoutOptions.Center, | ||
| HorizontalTextAlignment = TextAlignment.Center, | ||
| VerticalOptions = LayoutOptions.Center | ||
| }; | ||
| var grid = new Grid | ||
| { | ||
| RowDefinitions = | ||
| { | ||
| new RowDefinition { Height = new GridLength(80) }, | ||
| new RowDefinition { Height = new GridLength(40) } | ||
| } | ||
| }; | ||
|
|
||
| grid.Add(label, 0, 1); | ||
|
|
||
| border.Content = grid; | ||
| VisualStateManager.SetVisualStateGroups(border, new VisualStateGroupList | ||
| { | ||
| new VisualStateGroup | ||
| { | ||
| Name = "CommonStates", | ||
| States = | ||
| { | ||
| new VisualState { Name = "Normal" }, | ||
| new VisualState | ||
| { | ||
| Name = "Selected", | ||
| Setters = | ||
| { | ||
| new Setter { Property = Border.BackgroundColorProperty, Value = Colors.Blue }, | ||
| new Setter { Property = Border.StrokeProperty, Value = Colors.Red } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| return border; | ||
| }) | ||
| }; | ||
| _collectionView.AutomationId = "CollectionView"; | ||
|
|
||
| var layout = new VerticalStackLayout | ||
| { | ||
| Padding = new Thickness(30, 0), | ||
| Spacing = 25, | ||
| Children = { _collectionView } | ||
| }; | ||
| list.Add("a"); | ||
| list.Add("a"); | ||
| list.Add("a"); | ||
| list.Add("a"); | ||
| _collectionView.ItemsSource = list; | ||
|
|
||
| Content = new ScrollView | ||
| { | ||
| Content = layout | ||
| }; | ||
|
|
||
| } | ||
| } | ||
|
|
Binary file added
BIN
+9.51 KB
...TestCases.Mac.Tests/snapshots/mac/CollectionViewSelectionChangesVisualState.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions
25
src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue20062.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,25 @@ | ||
| #if TEST_FAILS_ON_WINDOWS // When selecting a single item, the selection background is applied to all items. | ||
| using NUnit.Framework; | ||
| using UITest.Appium; | ||
| using UITest.Core; | ||
|
|
||
| namespace Microsoft.Maui.TestCases.Tests.Issues; | ||
|
|
||
| public class Issue20062 : _IssuesUITest | ||
| { | ||
| public Issue20062(TestDevice device) : base(device) { } | ||
|
|
||
| public override string Issue => "CollectionView - SelectedItem visual state manager not working"; | ||
|
|
||
| [Test] | ||
| [Category(UITestCategories.CollectionView)] | ||
| public void CollectionViewSelectionChangesVisualState() | ||
| { | ||
| App.WaitForElement("CollectionView"); | ||
| var items = App.FindElementsByText("a"); | ||
| items.ElementAt(0).Tap(); | ||
| items.ElementAt(2).Tap(); | ||
| VerifyScreenshot(); | ||
| } | ||
| } | ||
| #endif | ||
Binary file added
BIN
+24.5 KB
...TestCases.iOS.Tests/snapshots/ios/CollectionViewSelectionChangesVisualState.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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.
Test failing on iOS:

Could you review it?
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.
I have updated the snapshot for iOS platform.