SfListView tap item programmatically

Hello

I am using Syncfusion.Xamarin NuGet packages v17.4.0.55.

I try to accomplish a programmatic tap on a SfListView item that behaves exactly the same as when manually tapping the item on the device itself.
Please note that I use MVVM.

For setting the items to select, I would use the following:
myListView.SelectedItems.Add(newItem);

However, this does not seem to work as expected:
Now my question is, if there is any way to mimic the manual tap exactly (including the highlighting in the ui and the SfListView "knowing" which items are already added)?


Kind regards
Phil

1 Reply

LN Lakshmi Natarajan Syncfusion Team March 19, 2020 06:04 PM UTC

Hi Phil, 
 
Thank you for using Syncfusion support. 
 
We have checked the reported query “SfListView tap item programmatically” from our end.  
 
#Regarding the UI does not set the background color of the selected item 
 
We would like to let you know that you can select an ListViewItem by the following ways, 
 
  1. Setting SelectedItem property
 
        private void Button_Clicked(object sender, EventArgs e) 
        { 
            var bc = (sender as Button).BindingContext as ContactsViewModel; 
            bc.SelectedItem = viewModel.contactsinfo[0]; 
        } 
 
  1. Adding item to the SelectedItems collection
 
        private void Button_Clicked(object sender, EventArgs e) 
        { 
            var bc = (sender as Button).BindingContext as ContactsViewModel; 
            listView.SelectedItems.Add(bc.contactsinfo[0]); 
        } 
 
In both cases, the UI will be updated as expected and the background color will be changed. 
 
Please refer SfListView selection in our online documents from following link, 
 
#Regarding the SelectionChanged event contains the item in the "AddedItems" list instead of the "RemovedItems" list 
 
We would like to let you know that when selecting, the selected item is added in the AddedItems collection. While deselecting, the deselected item will be added in the RemovedItems collection. 
 
ItemSelectionChangedEventArgs 
Description 
AddedItems 
Gets collection of the underlying data objects where the selection has been processed. 
 
RemovedItems 
Gets collection of the underlying data objects where the selection has been processed. 
 
 
Please refer SfListView selection in our online documents from following link, 
 
#Regarding highlighting in the ui and the SfListView "knowing" which items are already added 
 
You can achieve your requirement using the above solution to highlight the selection. Also, you can get the information of SelectedItem from AddedItems. 
 
We have already documented this scenario in our online document. Please refer the following link, 
 
We have attached the sample based on your requirement in the following link, 
 
Please check the sample and let us know if you still facing the same issue? If not, please modify our sample to reproduce the issue and revert us back with the following details which would be helpful for us to check on it and provide you the solution at the earliest. 
  • Share issue reproducing video
  • Share ListView related code snippet
  • Share code snippet of programmatic tap
 
Lakshmi Natarajan 
 


Loader.
Up arrow icon