question

StefanHodgesKluck-3493 avatar image
0 Votes"
StefanHodgesKluck-3493 asked WenyanZhang-MSFT commented

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);
     }


dotnet-xamarinformsdotnet-ios
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

May I know if you have got any chance to check my answer? Please feel free to share more detailed information to us and it will be very helpful for us to better work out this issue.

0 Votes 0 ·
WenyanZhang-MSFT avatar image
0 Votes"
WenyanZhang-MSFT answered WenyanZhang-MSFT commented

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 and IsEnabled="{Binding IsBusy, Converter={StaticResource negate}}" and SetListHeightRequest , 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.

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks for the reply. I looked into tweaking settings on IsEnabled and SetListHeightRequest, but found no different behavior.

However, I've managed to get the list responsive by adding CachingStrategy="RecycleElement" on the listview's properties.

0 Votes 0 ·

Does it work?

0 Votes 0 ·
StefanHodgesKluck-3493 avatar image
0 Votes"
StefanHodgesKluck-3493 answered WenyanZhang-MSFT commented

Yes, it works by adding CachingStrategy="RecycleElement" on the listview's properties

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you fo sharing this solution.

0 Votes 0 ·