Articles in this section
Category / Section

How to maintain the scroll position while updating ItemsSource at runtime in Xamarin.Forms ListView (SfListView)?

2 mins read

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

View sample in GitHub

Take a moment to peruse the documentation to learn more about scrolling in the SfListView with code examples.

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments
Please  to leave a comment
Access denied
Access denied