如何从Syncfusion列表视图中获取可见项?

问题描述

我正在Xamarin.Forms项目中工作,并使用Syncfusion SfListView控件获取列表。

现在,我只想在初始的基础上以及滚动列表视图后才能看到可见的项目。意味着在滚动停止后,我需要通过API获取可见项来进行更新。

如何在Xamarin.Forms Syncfusion SfListView中实现此目标?

有人可以为此提供解决方案吗?

谢谢。

解决方法

We have checked the reported query “How to get visible items from Syncfusion list view?” from our side. 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. Please find the code snippets from the below,C#: 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; 
            } 
        } 
    } 
,

SfListView允许在使用Changed事件滚动时进行通知。通过使用此事件,您可以基于LastBodyVisibleLineIndex属性和基础集合计数来确定是否到达SfListView中列表的最后一项。

您可以使用下面的代码。

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;
 }
}