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.
Unfortunately, activation email could not send to your email. Please try again.

How to maintain the same scroll position of ListView while updating ItemsSource at runtime?

By default, SfListView have scrolled to top automatically when changing the ItemsSource at runtime. However you can maintain the same scrolled position by using the ScrollY value of ExtendedScrollView from the VisualContainer. After changing the ItemsSource you can pass the ScrollY value to ScrollTo method of SfListView and scroll back to the same position.

For horizontal orientation, use the ScrollX value of ExtendedScrollView.

 By using Reflection, get the value of VisualContainer from SfListView and use the same way to get the ExtendedScrollView from VisualContainer. Please refer the below code snippets.

C#

 

Click here to download the sample.

Article ID: Published Date: Last Revised Date: Platform: Control:
8029 10/06/2017 07/20/2018 Xamarin.Forms SfListView
Did you find this information helpful?
Comments
Samir Asaf Nov 15, 2017

The above example works fine. However a scroll is animated when changing the ItemsSource with above code? Is there any way to avoid the animation?

Reply
Muthu Kumaran Gnanavinayagam [Syncfusion] Nov 16, 2017

Hi Samir,

 

We have checked the reported query “Need to disable the scrolling animation while changing the ItemsSource” from our side. You can achieve your requirement by using ScrollToRowIndex method where we have provided option to disable to animation while scrolling. You can get the value of ScrollLineIndex from the ScrollAxisBase in VisualContainer of SfListView before changing the ItemsSource. You can pass the value of ScrollLineIndex to ScrollToRowIndex and disable the animation while scrolling down the list as like below code example.

 

Code example[C#]:

public partial class MainPage : ContentPage

    {

        VisualContainer visualContainer;

 

        public MainPage()

        {

            InitializeComponent();

            visualContainer = listView.GetVisualContainer();

        }

 

        private void ChangeItemsSource_Clicked(object sender, EventArgs e)

        {

            var scrollLineIndex = visualContainer.ScrollRows.ScrollLineIndex;

            var viewmodel = new ContactsViewModel();

            listView.ItemsSource = viewmodel.EmployeeInfo;

            listView.LayoutManager.ScrollToRowIndex(scrollLineIndex, true);

        }

    }

 

Note: This code example is applicable only after SfListView (v15.4.017).

 

Please let us know if you require further assistance.

 

Regards,

G.Muthu Kumaran.

Reply
Add Comment
You must log in to leave a comment

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.