From 28237b7b27ef318b2c811e9829c2442613c42feb Mon Sep 17 00:00:00 2001 From: adrianknight89 Date: Mon, 6 Feb 2017 05:59:11 -0600 Subject: [PATCH] [iOS/Android] Handle item selection highlight when ListView cell is tapped erratically (#520) * Fix multi tap selection highlight * added sample code * add ios fix --- .../Bugzilla32956.cs | 48 +++++++++++++++++++ ...rin.Forms.Controls.Issues.Shared.projitems | 1 + .../Renderers/ListViewAdapter.cs | 3 +- .../Renderers/ListViewRenderer.cs | 3 +- 4 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla32956.cs diff --git a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla32956.cs b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla32956.cs new file mode 100644 index 000000000..b1baafe57 --- /dev/null +++ b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla32956.cs @@ -0,0 +1,48 @@ +using System.Collections.Generic; +using Xamarin.Forms.CustomAttributes; +using Xamarin.Forms.Internals; +using System.Threading.Tasks; + +#if UITEST +using Xamarin.UITest; +using NUnit.Framework; +#endif + +namespace Xamarin.Forms.Controls +{ + [Preserve(AllMembers = true)] + [Issue(IssueTracker.Bugzilla, 32956, "Setting ListView.SelectedItem to null does not remove list item highlight when list item is tapped multiple times quickly", PlatformAffected.Android | PlatformAffected.iOS)] + public class Bugzilla32956 : TestNavigationPage + { + protected override void Init() + { + var list = new List(); + for(var i=0; i<10; i++) + list.Add(i); + + var listView = new ListView + { + ItemsSource = list + }; + listView.ItemSelected += async (sender, args) => + { + if (args.SelectedItem == null) + return; + + await Task.Delay(1000); + await Navigation.PushAsync(new ContentPage()); + }; + + var contentPage = new ContentPage + { + Content = listView + }; + contentPage.Appearing += (sender, args) => + { + listView.SelectedItem = null; + }; + + PushAsync(contentPage); + } + } +} \ No newline at end of file diff --git a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems index 859f3c085..e55302a95 100644 --- a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems +++ b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems @@ -58,6 +58,7 @@ Code + diff --git a/Xamarin.Forms.Platform.Android/Renderers/ListViewAdapter.cs b/Xamarin.Forms.Platform.Android/Renderers/ListViewAdapter.cs index 245c3d742..1ff11f4a8 100644 --- a/Xamarin.Forms.Platform.Android/Renderers/ListViewAdapter.cs +++ b/Xamarin.Forms.Platform.Android/Renderers/ListViewAdapter.cs @@ -371,8 +371,9 @@ namespace Xamarin.Forms.Platform.Android if (position < 0 || position >= Count) return; + if(_lastSelected != view) + _fromNative = true; Select(position, view); - _fromNative = true; Controller.NotifyRowTapped(position, cell); } diff --git a/Xamarin.Forms.Platform.iOS/Renderers/ListViewRenderer.cs b/Xamarin.Forms.Platform.iOS/Renderers/ListViewRenderer.cs index 9a6e2cf70..be95731b5 100644 --- a/Xamarin.Forms.Platform.iOS/Renderers/ListViewRenderer.cs +++ b/Xamarin.Forms.Platform.iOS/Renderers/ListViewRenderer.cs @@ -914,7 +914,8 @@ namespace Xamarin.Forms.Platform.iOS SetCellBackgroundColor(cell, UIColor.Clear); - _selectionFromNative = true; + if (!cell.Selected) + _selectionFromNative = true; tableView.EndEditing(true); Controller.NotifyRowTapped(indexPath.Section, indexPath.Row, formsCell);