Articles in this section
Category / Section

How to identify when end of the list is reached on scrolling in ListView?

1 min read

SfListView notify the users while scrolling using Changed event of ScrollAxisBase in VisualContainer of SfListView. By using this event, you find whether reached the last item in the list in SfListView based on LastBodyVisibleLineIndex property and underlying collection count.

By using Reflection, get the VisualContainer from SfListView and use the same way to get ScrollAxisBase from VisualContainer. Please refer the below code snippet.

C#

public partial class MainPage : ContentPage
{
        ScrollAxisBase scrollRows;
        bool isAlertShown= false ;
 
        public MainPage()
        {
            InitializeComponent();
            VisualContainer visualContainer = listView.GetType().GetRuntimeProperties().First(p => p.Name == "VisualContainer").GetValue(listView) as VisualContainer;
            scrollRows = visualContainer.GetType().GetRuntimeProperties().First(p => p.Name == "ScrollRows").GetValue(visualContainer) as ScrollAxisBase;
            scrollRows.Changed += ScrollRows_Changed;
        }
 
        ///<summary>
        ///To notify when end reached
        ///</summary>
        private void ScrollRows_Changed(object sender, ScrollChangedEventArgs e)
        {
            var lastIndex = scrollRows.LastBodyVisibleLineIndex;
 
            //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;
            var totalItems = listView.DataSource.DisplayItems.Count + header + footer;
 
            if ((lastIndex == totalItems - 1))
            {
                if (!isAlertShown)
                {
                    DisplayAlert("Alert", "End of list reached...", "Ok");
                    isAlertShown= true;
                }
            }
            else
                isAlertShown= false;
        }  
}

 

Click here to download the sample.

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments
Please sign in to leave a comment
Access denied
Access denied