c# - Xamarin.Forms ListView 加载更多

标签 c# xamarin.forms

问题

我想达到的效果你基本能看到这里吗

enter image description here

所以当用户滚动到末尾时我想加载更多,因为我的列表非常大并且我想最大化性能。

我正在尝试按如下方式实现此目的,将主集合与数据分开,以便我可以在用户到达末尾时设置新的 ItemSource。

到目前为止我实现了什么

 public class ViewModel : BaseViewModel {

        public ViewModel() {
            Initialize();
        }

        public List<List<Usermodel>> SplitedUserLists { get; set; }

        //Main List that im Binding to
        public List<Usermodel> ItemSourceCollection { get; set; }

        public int ChunkSize { get; set; }
        #endregion

        private async void Initialize() {

            ItemSourceCollection = await LoadList();

            // Splites the list (in this case the chunk Size is 5)
            SplitedScoreLists = ItemSourceCollection.Split(GetChunkSize()));
            ItemSourceCollection = SplitedScoreLists[0];
        }

        //Gets called from CodeBehind
        public void ListViewItemAppearing(ItemVisibilityEventArgs e) {

            //Bottom Hit!
            if (e.Item == ItemSourceCollection[ItemSourceCollection.Count - 1]) {

                if (ChunkSize >= SplitedScoreLists.Count) {
                    return;
                }

                foreach (var usermodel in SplitedScoreLists[ChunkSize].ToList()) {
                    ItemSourceCollection.Add(usermodel);
                }

                if (ChunkSize < SplitedScoreLists.Count) {
                    ChunkSize++;
                }
            }
        }
 }

问题

  1. 我的实现的问题是,由于重复,列表实际上比原始列表的计数长。

  2. 这是实现类似目标的正确方法吗?

  3. 我真的用这个提高了性能吗?我需要使列表包含 1000 多个条目。

最佳答案

关于如何实现这一点有很好的教程:

http://motzcod.es/post/107620279512/load-more-items-at-end-of-listview-in

https://github.com/jguibault/Xamarin-Forms-Infinite-Scroll

http://www.codenutz.com/lac09-xamarin-forms-infinite-scrolling-listview/

关键是什么时候引发“加载更多”命令:

public class InfiniteListView : ListView
{
    public static readonly BindableProperty LoadMoreCommandProperty = BindableProperty.Create<InfiniteListView, ICommand>(bp => bp.LoadMoreCommand, default(ICommand));

    public ICommand LoadMoreCommand
    {
        get { return (ICommand) GetValue(LoadMoreCommandProperty); }
        set { SetValue(LoadMoreCommandProperty, value); }
    }

    public InfiniteListView()
    {
        ItemAppearing += InfiniteListView_ItemAppearing;
    }

    void InfiniteListView_ItemAppearing(object sender, ItemVisibilityEventArgs e)
    {
        var items = ItemsSource as IList;

        if (items != null && e.Item == items[items.Count - 1])
        {
            if(LoadMoreCommand != null && LoadMoreCommand.CanExecute(null))
                LoadMoreCommand.Execute(null);
        } 
    }
}

关于c# - Xamarin.Forms ListView 加载更多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43102069/

相关文章:

c# - 在按钮 WPF 中添加带有 fontawesome 的文本

c# - 无法打开作为链接添加的 configSource 文件

c# - CS1998 抛出错误而不是警告

c# - 如何在 Xamarin Forms 中将 BackgroundImage 添加到 ListView?

android - 是否有构建 Xamarin.Forms 项目的最有效方法?

c# - System.Threading.Timer 和 CA1823 :AvoidUnusedPrivateFields

c# - Microsoft.AspNet.Identity.Core 和 Microsoft.AspNetCore.Identity 之间有什么区别?

xamarin - 将 OnAppearing 传递给 Xamarin Forms MVVM 中的 ViewModel?

xamarin.forms - 错误 : Unable to generate deps. json,它可能已经生成。添加对 Xamarin 表单的服务引用时

ios - Xamarin Forms - TapGestureRecognizer 不适用于 iOS