Category / Section
How to identify when end of the list is reached on scrolling in .NET MAUI ListView (SfListView) ?
1 min read
The .NET MAUI ListView (SfListView) allows you to identify when the end of the list is reached when scrolling. By using the Changed event of ScrollAxisBase in VisualContainer, you can find whether you have reached the last item in the list based on the LastBodyVisibleLineIndex property and underlying collection count.
C#
You can get the item elements to hold by a scrollable visual container using the GetVisualContainer helper method.
public class ListViewBehavior : Behavior<SfListView> { SfListView ListView; VisualContainer VisualContainer; bool isAlertShown = false; int totalItems = 0; protected override void OnAttachedTo(SfListView bindable) { ListView = bindable; ListView.Loaded += ListView_Loaded; VisualContainer = ListView.GetVisualContainer(); VisualContainer.ScrollRows.Changed += ScrollRows_Changed; base.OnAttachedTo(bindable); } private void ListView_Loaded(object sender, EventArgs e) { //To include header if used var header = (ListView.HeaderTemplate != null && !ListView.IsStickyHeader) ? 1 : 0; //To include footer if used var footer = (ListView.FooterTemplate != null && !ListView.IsStickyFooter) ? 1 : 0; totalItems = ListView.DataSource.DisplayItems.Count + header + footer; } private void ScrollRows_Changed(object sender, ScrollChangedEventArgs e) { var lastIndex = VisualContainer.ScrollRows.LastBodyVisibleLineIndex; if (lastIndex != -1 && (lastIndex == totalItems - 1)) { if (!isAlertShown) { App.Current.MainPage.DisplayAlert("Alert", "End of list reached...", "Ok"); isAlertShown = true; } } else isAlertShown = false; } }
Take a moment to peruse the documentation to learn more about scrolling in SfListView with code examples.