Yes, it works by adding CachingStrategy="RecycleElement"
on the listview's properties
ListView Item Selected/Tapped not Working on iOS
We have recently implemented ItemSelected
on one of our ListView components. It works fine on Android, but on iOS the list items only respond to a user hitting them about 1 out of 10 times. We tried changing our ItemSelected
handler to an ItemTapped
, but ran into the same issue.
Here are the attributes on our ListView
:
x:Name="list"
ItemsSource="{Binding Items}"
SeparatorVisibility="None"
HasUnevenRows="False"
RowHeight="100"
BackgroundColor="Transparent"
ItemSelected="OnItemSelected"
IsEnabled="{Binding IsBusy, Converter={StaticResource negate}}"
And here is the relevant code behind (though the OnItemSelected
event is not fired when tapping, so I think it's failing in the xaml):
protected override void OnAppearing()
{
SetListHeightRequest(list, pageTitle, CTAs);
base.OnAppearing();
}
async void OnItemSelected(object sender, SelectedItemChangedEventArgs args)
{
_viewModel.SelectedItem = args.SelectedItem;
await _viewModel.HandleSelectItemAsync();
}
protected void SetListHeightRequest(ListView list, Layout title, Layout ctas)
{
list.HeightRequest = Application.Current.MainPage.Height - (title.Height + ctas.Height);
}
3 answers
Sort by: Most helpful
-
Stefan Hodges-Kluck 6 Reputation points
2021-11-08T13:43:53.55+00:00 -
Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 35,916 Reputation points Microsoft External Staff
2021-10-28T08:32:48.477+00:00 Hello,
Welcome to our Microsoft Q&A platform!
I'm so sorry I can't reproduce this issue, I suspect that it caused by the ViewModel, so I display a tableview without
ViewModel
, it works well.
XAML<ListView x:Name="list" ItemsSource="{Binding Items}" SeparatorVisibility="None" HasUnevenRows="False" RowHeight="100" BackgroundColor="Transparent" ItemSelected="OnItemSelected"> <ListView.ItemTemplate> <DataTemplate> <TextCell Text="{Binding }" /> </DataTemplate> </ListView.ItemTemplate> </ListView>
Code behind
public ObservableCollection<string> Items { get; set; } = new ObservableCollection<string>(); public MainPage() { InitializeComponent(); for (int i = 0; i < 10; i++){ Items.Add(i.ToString()); } BindingContext = this; } protected override void OnAppearing() { base.OnAppearing(); } async void OnItemSelected(object sender, SelectedItemChangedEventArgs args) { Console.WriteLine("has been selected"); }
Then, I add the ViewMode and Model, it also works.
XAML
<DataTemplate> <TextCell Text="{Binding Text}" /> </DataTemplate>
Code behind
public partial class MainPage : ContentPage { public ItemViewModel Items { get; set; } = new ItemViewModel(); public MainPage() { InitializeComponent(); BindingContext = Items; } protected override void OnAppearing() { base.OnAppearing(); } async void OnItemSelected(object sender, SelectedItemChangedEventArgs args) { Items.SelectedItem = (Item)args.SelectedItem; await Items.TaskHandleSelectItemAsync(); } }
ItemViewModel
public class ItemViewModel { public ObservableCollection<Item> Items { get; set; } = new ObservableCollection<Item>(); public Item SelectedItem { get; set; } public ItemViewModel() { for (int i = 0; i < 10; i++) { Item item = new Item { Text = i.ToString() }; Items.Add(item); } } public async Task TaskHandleSelectItemAsync() { Console.WriteLine("has been selected" + SelectedItem.Text); } }
Model
public class Item { public string Text { get; internal set; } }
I change the HeightRequest, it also works. The biggest difference is that you use
_viewModel
andIsEnabled="{Binding IsBusy, Converter={StaticResource negate}}"
andSetListHeightRequest
, you could try to check these methods.
In addition, would you mind sharing a demo to me so that I can reproduce this issue and test it?Best Regards,
Wenyan Zhang
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread. -
Gautam Jain 0 Reputation points
2025-02-27T08:00:06.5366667+00:00 Such basic issue still exists in year 2025. It is a shame! Thanks @Stefan Hodges-Kluck . Your solution works.