0

使用增量加载在 Xamarin.Forms ListView/CollectionView 上加载分组集合的最佳方式是什么?

例如,假设一个集合包含几个组,每个组包含一个项目列表。

[
    [123, 143, 341234, 234234, 514232, 23511, 673456, ...],
    [12, 143, 341234, 234234, 514232, 23511, , ...],
    [12, 143, 341234, 234234, 514232, 23511, 313, ...],
    [12, 143, 341234, 514232, 23511, 673456, ...],
    [12, 143, 341234, 234234, 514232, 132, 23511, 673456, ...],
    .
    .
    .
    [12, 143, 341234, 234234, 514232, 23511, 673456, ...],
]

更新

ListView.ItemAppearing使用一维列表,我可以使用/CollectionView.RemainingItemsThresholdReached事件将数据加载到 ListView 或 CollectionView 中。

使用 Xamarin.Forms CollectionView 无限滚动

在 Xamarin.Forms 的 ListView 末尾加载更多项目

listView.ItemAppearing += ListView_ItemAppearing;
IList<Item> originalList = new List<Item> {Item1, ..., Item10000};

private void ListView_ItemAppearing(object sender, ItemVisibilityEventArgs e)
{
    if (// all the items in the original list loaded into the listview) 
    {
        listView.ItemAppearing -= ListView_ItemAppearing;
    }
    else
    {
        // Add next set of items from the original list to the listview
    }
}

所以我担心的是,将分组集合增量加载到ListViewor中的最佳方法(或最佳实践)是CollectionView什么?

4

1 回答 1

1

为了加载分组集合,我在 ItemsSource 中添加了一些附加数据,例如:

    PersonViewModel pvm = new PersonViewModel();
           private  ObservableCollection<PersonGroup> myitems = new ObservableCollection<PersonGroup>();
            public TestGroup()
            {
    
                InitializeComponent();
                myitems = pvm.Persons;
                //set collectionview
                mycol.ItemsSource =myitems;
                mycol.RemainingItemsThreshold = 2;
                mycol.RemainingItemsThresholdReached += Mycol_RemainingItemsThresholdReached;
            }
    
            private void Mycol_RemainingItemsThresholdReached(object sender, EventArgs e)
            {
               
                for (int i = 0; i <= 3; i++)
                {
                    myitems.Add(new PersonGroup("plusgroup", new List<Person> { new 
                    Person { name="plus1",
                    address="11111"},
                   new Person { name="plus2",
                    address="22222"}
    
                     }));
                }
            }

这是我的视图模型和模型:

public class PersonViewModel {


    public ObservableCollection<PersonGroup> Persons { get; set; } = new ObservableCollection<PersonGroup>();

    public List<Person> peoplelist = new List<Person>();

    public PersonViewModel()

    {

        for (int k = 1; k <= 3; k++)

        {

           peoplelist.Add(new Person
            {
                name = "People" + k.ToString(),

                address = "somewhere"+k.ToString()
            });

        }


        for (int i = 1; i < 50; i++)

        {

            Persons.Add(new PersonGroup("Group" + i.ToString(), peoplelist));

        }

    }
}

模型:

public class PersonGroup{
    public string GroupName { set; get; }
    
            public  PersonGroup(string name,List<Person>persons):base(persons)
            {
                GroupName = name;
            }
    
        }

结果:

在此处输入图像描述

于 2021-08-12T01:39:19.170 回答