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

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

Platform: Xamarin.Forms |
Control: SfListView |
Published Date: October 6, 2017 |
Last Revised Date: July 20, 2018

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.

2X faster development

The ultimate Xamarin UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment
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

Please sign in to access our KB

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