We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: November 16, 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

SfListView - Scroll to last item added - MVVM

Thread ID:

Created:

Updated:

Platform:

Replies:

143124 Mar 5,2019 12:44 PM UTC Mar 7,2019 10:08 AM UTC Xamarin.Forms 3
loading
Tags: SfListView
Øystein Günther
Asked On March 5, 2019 12:44 PM UTC

Is it possible to automatically scroll to the last item added to a listview in a mvvm setup? My requirement is to make sure that the last item added in my vertical listview is always visible. Behavior?

Gnana Priya Namasivayam [Syncfusion]
Replied On March 6, 2019 12:19 PM UTC

Hi Øystein, 
 
Thanks for using Syncfusion product. 
 
We have checked the reported query from our side. We would like to let you know that you can scroll the listview to newly added item in MVVM pattern as mentioned below. We have prepared the sample based on your requirement, please find the sample from below. 
 
 
XAML: 
 
<Grid> 
   <Button Text="Add Item" Command="{Binding TapCommand}" CommandParameter="{x:Reference listView}"/> 
   <syncfusion:SfListView/> 
</Grid> 
 
ViewModel: 
 
public class ContactsViewModel : INotifyPropertyChanged 
{ 
  public Command<object> TapCommand { get; set; } 
  public ContactsViewModel() 
  { 
    contactsinfo = new ObservableCollection<Contacts>(); 
    TapCommand = new Command<object>(OnTapped); 
   } 
   private void OnTapped(object obj) 
   { 
     listView = obj as SfListView; 
     Contacts newContact = new Contacts(); 
     newContact.ContactName = "Jonas"; 
     contactsinfo.Add(newContact); 
     int newIndex = listView.DataSource.DisplayItems.IndexOf(newContact); 
     listView.LayoutManager.ScrollToRowIndex(newIndex, true); 
   } 
} 
            
  
Please let us know whether sample meets your requirement. 
 
Regards, 
Gnana Priya N 


Øystein Günther
Replied On March 6, 2019 02:18 PM UTC

Hi Gnana,

Thank you so much for providing a sample on how to accomplish this. 
What actually did the trick was your: listView.LayoutManager.ScrollToRowIndex(newIndex, true); 
As opposed to what I had: listView.ScrollTo(<object>, Syncfusion.ListView.XForms.ScrollToPosition.MakeVisible, true); 

However, your sample is based on sending a reference of the ListView into the ViewModel. Wouldn't you say that this violates the MVVM pattern?
In my opinion a ViewModel should never access a UI element.

Not quite sure about how to go about it, but what I had in mind was a View with two ListViews, let's say A and B.
ListView A is on the left side and serves as kind of like "select an object from the list". When the user selects an object from list A, I was thinking that this would trigger a "ListViewA_ItemTapped" event in a connected behavior. This event would then execute a command in the ViewModel that adds the object to list B. When that task is complete, the "behavior" event would then perform the ScrollToRowIndex() method on B, in order to always have the last item added visible. Your thoughts?

Subburaj Pandian Veluchamy [Syncfusion]
Replied On March 7, 2019 10:08 AM UTC

Hi Øystein,  
 
Thank you for the update. 
  
We would like to let you know that you cannot pass the ItemIndex as a parameter to ScrollTo method, inorder to pass the added item index you can use ScrollToRowIndex method which scrolls the view to added item. Meanwhile, If you have any issue to use listview in ViewModel then you can also write `Behavior` for listview to achieve your requirement as you mentioned.  
 
Please find the sample in which we have added items and accessed listview in Behavior class, attached sample for your reference. 
  
  
To know more about difference between ScrollTo and ScrollToRowIndex method, you can refer the following user guide documentation link. 
 
  
Please let us know, if you have any query. 
 
Regards,
Subburaj Pandian V
 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

;