简体   繁体   English

Xamarin.Forms ListView加载更多

[英]Xamarin.Forms ListView Load More

The Problem 问题

What I want to achieve can you basically see here 我想要实现的目标基本上可以在这里看到

在此输入图像描述

so when the user scrolls to the end I want to load more, because my List is very large and I want to Maximize Performance. 因此,当用户滚动到最后我想加载更多,因为我的列表非常大,我想最大化性能。

I'm trying to achieve this as follows, in splitting the Main Collection with the Data so that i can set the ItemSource new when the User reaches the end. 我正在尝试按如下方式实现此目的,将主集合与数据分开,以便我可以在用户到达结尾时将ItemSource设置为新的。

What ive Implemented so far 到目前为止我实施了什么

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

Questions 问题

  1. The problem with my Implementation is that the List is actually longer than the Count of the original List due to duplicates. 我的实现的问题是,由于重复,List实际上比原始List的Count长。

  2. Is this the right way to achieve something like this? 这是实现这样的事情的正确方法吗?

  3. Am I actually increasing Performance with this? 我真的在提高性能吗? I need to cause the List is 1000+ entries. 我需要使List是1000多个条目。

There are nice tutorials on how to achieve this: 有很好的教程如何实现这一目标:

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

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

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

The key point is when to raise the "load more" command: 关键点是什么时候提高“加载更多”命令:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM