Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Commit

Permalink
Issue 15647: Fix selecting of an item in an Android ListView (#15660)
Browse files Browse the repository at this point in the history
* Add repro sample (#15647)

* Fix selecting of an item in an Android ListView (#15647)
  • Loading branch information
heikow10 committed Nov 9, 2023
1 parent dad72d3 commit 6153378
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
using Xamarin.Forms.CustomAttributes;
using Xamarin.Forms.Internals;
using System.Collections.ObjectModel;
using System;

#if UITEST
using Xamarin.Forms.Core.UITests;
using Xamarin.UITest;
using NUnit.Framework;
#endif

namespace Xamarin.Forms.Controls.Issues
{
#if UITEST
[Category(UITestCategories.ManualReview)]
#endif
[Preserve(AllMembers = true)]
[Issue(IssueTracker.Github, 15647, "[Bug] Android ListView SelectedItem highlighting", PlatformAffected.Android)]
public class Issue15647 : TestContentPage
{
protected override void Init()
{
ViewModelIssue15647 viewModelIssue15647 = new ViewModelIssue15647();

Label testInstructionsLabel = new Label()
{
HorizontalTextAlignment = TextAlignment.Center,
Padding = new Thickness(3.0),
Text = "Click on the buttons to select the first or second item of the ListView." + Environment.NewLine +
"The correct item has to be marked as selected so that this test is successful.",
};

DataTemplate itemDataTemplate = new DataTemplate(() =>
{
Label itemLabel = new Label();
itemLabel.SetBinding(Label.TextProperty, ".");
return new ViewCell()
{
View = itemLabel,
};
});
ListView itemsListView = new ListView()
{
// Header = "Header",
ItemTemplate = itemDataTemplate,
};
itemsListView.SetBinding(ListView.ItemsSourceProperty, nameof(ViewModelIssue15647.Items));

Button selectFirstItemButton = new Button()
{
Text = "Select first item",
};
selectFirstItemButton.Clicked += (_, __) =>
{
Console.WriteLine(itemsListView.SelectedItem);
itemsListView.SelectedItem = viewModelIssue15647.Items[0];
Console.WriteLine(itemsListView.SelectedItem);
};

Button selectSecondItemButton = new Button()
{
Text = "Select second item",
};
selectSecondItemButton.Clicked += (_, __) =>
{
Console.WriteLine(itemsListView.SelectedItem);
itemsListView.SelectedItem = viewModelIssue15647.Items[1];
Console.WriteLine(itemsListView.SelectedItem);
};

Content = new StackLayout()
{
Children = { testInstructionsLabel, selectFirstItemButton, selectSecondItemButton, itemsListView },
};

BindingContext = viewModelIssue15647;
}
}

[Preserve(AllMembers = true)]
public class ViewModelIssue15647
{
public ViewModelIssue15647()
{
Items = new ObservableCollection<string>() { "first item", "second item" };
}

public ObservableCollection<string> Items { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1871,6 +1871,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Issue15305.xaml.cs">
<DependentUpon>Issue15305.xaml</DependentUpon>
</Compile>
<Compile Include="$(MSBuildThisFileDirectory)Issue15647.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Controls\MyCollectionView.xaml.cs">
<DependentUpon>MyCollectionView.xaml</DependentUpon>
<SubType>Code</SubType>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,7 @@ void SelectItem(object item)
int position = TemplatedItemsView.TemplatedItems.GetGlobalIndexOfItem(item);
AView view = null;
if (position != -1)
view = _realListView.GetChildAt(position + 1 - _realListView.FirstVisiblePosition);
view = _realListView.GetChildAt(position + _realListView.HeaderViewsCount - _realListView.FirstVisiblePosition);

Select(position, view);
}
Expand Down

0 comments on commit 6153378

Please sign in to comment.