Category / Section
How to automatically scroll to bring a selected item into the view?
1 min read
SfListView allows you to automatically bring the selected item into the view when it changed at runtime by calling the ScrollToRowIndex method.
In linear layout, you can get the row index of SelectedItem using the DisplayItems.IndexOf method and adjust the index based on header and group header like below code snippets.
C#
public partial class MainPage : ContentPage { public MainPage() { InitializeComponent(); listView.PropertyChanged += listView_PropertyChanged; } private void listView_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) { if (e.PropertyName == "SelectedItem") { var selectedItemIndex = listView.DataSource.DisplayItems.IndexOf(listView.SelectedItem); selectedItemIndex += (listView.HeaderTemplate != null && !listView.IsStickyHeader || !listView.IsStickyGroupHeader) ? 1 : 0; selectedItemIndex -= (listView.GroupHeaderTemplate != null && listView.IsStickyGroupHeader) ? 1 : 0; (listView.LayoutManager as LinearLayout).ScrollToRowIndex(selectedItemIndex); } } }
Click here to download the sample.