Using the Xamarin.Forms 1.3.0 ListView.ScrollTo Method

Xamarin.Forms v1.3.0 has a really great addition which allows us to scroll to a specific location within a ListView.

ScrollTo works by performing a linear search for an individual item, or an individual item within a specified group.

There are two variations:

ScrollTo(object, ScrollToPosition, bool)

Scrolls the ListView to the item.

object is an item from the ItemSource used to populate the ListView.

ScrollToPosition allows you to show position the item at the:

  • Top of the screen (Start)
  • Bottom of the screen (End)
  • Center of the screen (Center)
  • Or just scroll until the item is visible on the screen (MakeVisible)

bool is whether the scrolling should be animated.

 

ScrollTo(object, object, ScrollToPosition, bool)

Scrolls the ListView to the item in the group

This variation works exactly like the first version but also allows you to specify the group to search within (the second parameter).

Here is how the code looks when attached to an Add button associated with the ListView:

buttonAdd.Clicked += (sender, e) =>
{
    var person = new Person(string.Format("Name {0}", Count++), DateTime.Today, Color.Blue);

    people.Add(person);

    listView.ScrollTo(person, ScrollToPosition.End, true);

};

This sample will add a new item to a ListView then scroll the ListView so that the user can see the newly added item.

Here is another example, which is tied to a button named Bottom:

buttonScrollToBottom.Clicked += (sender, e) =>
{
    var person = people[people.Count-1];

    listView.ScrollTo(person, ScrollToPosition.End, true);
};

This gets the last item in the list and scrolls the ListView to that item.

 

You can see this code in action by downloading my 1.3.0 sample on GitHub.

Leave a Reply 2 comments