Sulay Joshi
Sulay Joshi

Reputation: 195

How to get visible items from Syncfusion list view?

I am working in Xamarin.Forms project and using Syncfusion SfListView control for a list.

Now I want to get visible items only on initial base and after scrolling the list view. Means after scrolling stop I need the visible items to update by getting updates through API.

How can I achieve this in Xamarin.Forms Syncfusion SfListView?

Could anyone please give the solution for it?

Thanks.

Upvotes: 0

Views: 778

Answers (2)

SaiGanesh Sakthivel
SaiGanesh Sakthivel

Reputation: 49

We would like to inform you that you can get the items created on load from the Children property of the VisualContainer. Also, you can get the information of the last visible item using the VisualContainer.ScrollRows.LastBodyVisibleLineIndex property.

The VisualContainer.Children will have the details of the visible items.

public class Behavior : Behavior<ContentPage> 
    { 
        SfListView ListView; 
        VisualContainer VisualContainer; 
     
        protected override void OnAttachedTo(ContentPage bindable) 
        { 
            ListView = bindable.FindByName<SfListView>("listView"); 
            VisualContainer = ListView.GetVisualContainer(); 
            ListView.ScrollStateChanged += ListView_ScrollStateChanged; 
            base.OnAttachedTo(bindable); 
        } 
     
        private void ScrollView_Scrolled(object sender, ScrolledEventArgs e) 
        { 
        } 
     
        private void ListView_ScrollStateChanged(object sender, ScrollStateChangedEventArgs e) 
        { 
            if (e.ScrollState == ScrollState.Idle) 
            { 
                var visibleItems = VisualContainer.Children; 
                var lastVisibleItem = VisualContainer.ScrollRows.LastBodyVisibleLineIndex; 
            } 
        } 
    } 

Upvotes: 1

Wendy Zang - MSFT
Wendy Zang - MSFT

Reputation: 10938

The SfListView allows notifying when scrolling using the Changed event. By using this event, you can find whether reached the last item in the list in the SfListView based on the LastBodyVisibleLineIndex property and underlying collection count.

You could use the code below.

using Syncfusion.ListView.XForms.Control.Helpers;
public partial class MainPage : ContentPage
{
 VisualContainer visualContainer;
bool isAlertShown = false;

public MainPage()
{
    InitializeComponent();
    visualContainer = listView.GetVisualContainer();
    visualContainer.ScrollRows.Changed += ScrollRows_Changed;
}

///<summary>
///To notify when end reached
///</summary>
private void ScrollRows_Changed(object sender, ScrollChangedEventArgs e)
{
    var lastIndex = visualContainer.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)
        {
           // do something to update the visible items by getting updates through API.
            DisplayAlert("Alert", "End of list reached...", "Ok");
            isAlertShown = true;
        }
    }
    else
        isAlertShown = false;
 }
}

Upvotes: 0

Related Questions