How to maintain the scroll position while updating ItemsSource at runtime in Xamarin.Forms ListView (SfListView)?
The Xamarin.Forms ListView (SfListView) is scrolled to the top automatically when changing the ItemsSource at runtime by default. However, you can maintain the same scrolled position by using the ScrollY value of the ExtendedScrollView. After changing the ItemsSource pass the ScrollY value to the ScrollTo method and scroll back to the same position.
For horizontal orientation, use the ScrollX value of the ExtendedScrollView.
C#
Get the ExtendedScrollView by using the GetScrollView helper method, which is accessible through the Syncfusion.ListView.XForms.Control.Helpers namespace. The ExtendedScrollView is the customized ScrollView used in the SfListView.
using Syncfusion.ListView.XForms.Control.Helpers; public partial class MainPage : ContentPage { ExtendedScrollView scrollview; public MainPage() { InitializeComponent(); scrollview = listView.GetScrollView(); } private void ChangeItemsSource_Clicked(object sender, EventArgs e) { var viewmodel = new ContactsViewModel(); listView.ItemsSource = viewmodel.EmployeeInfo; listView.ScrollTo(scrollview.ScrollY); } }
Take a moment to peruse the documentation to learn more about scrolling in the SfListView with code examples.