Hello @Tharindu Perera ,
Welcome to our Microsoft Q&A platform!
to load incrementally a grouped collection into a ListView or CollectionView
ListView
You could still achieve the function in the listView's ItemAppearing event. When the listView is grouped, the itemIndex is from the items of all the groups and the items also contain the group line. Try to caculate the count of all the items and then detect the value of ItemIndex.
Here is the sample code, you could refer to it.
public partial class TestPage : ContentPage
{
public ObservableCollection<ViewGroup> DataCollection { get; set; }
int number = 0;
public TestPage()
{
InitializeComponent();
DataCollection = new ObservableCollection<ViewGroup>();
DataCollection.CollectionChanged += DataCollection_CollectionChanged;
//add items to the dataCollection
BindingContext = this;
}
private void DataCollection_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
number = 0;
foreach (ViewGroup item in DataCollection)
{
number += item.Count + 1; //caculate all the items, 1 is the each group line
}
}
private void listView_ItemAppearing(object sender, ItemVisibilityEventArgs e)
{
if (e.ItemIndex == number - 1)
{
DataCollection.Add(new ViewGroup("group_" + (DataCollection.Count + 1), new ObservableCollection<TestModel>() {
//items
}));
}
}
}
<ListView ItemsSource="{Binding DataCollection}"
GroupDisplayBinding="{Binding GroupTitle}"
HasUnevenRows="True"
ItemAppearing="listView_ItemAppearing"
IsGroupingEnabled="True">
<ListView.ItemTemplate>
...
</ListView.ItemTemplate>
</ListView>
The group model class
public class ViewGroup : Collection<TestModel>
{
public string GroupTitle { get; set; }
public ViewGroup(string groupTitle, IList<TestModel> list) : base(list)
{
this.GroupTitle = groupTitle;
}
}
CollectionView
We can just achieve the infinite scrolling in a grouped collectionView using RemainingItemsThresholdReached
event as in the normal collectionView.
<CollectionView
ItemsSource="{Binding DataCollection}"
RemainingItemsThresholdReached="CollectionView_RemainingItemsThresholdReached"
RemainingItemsThreshold="1"
IsGrouped="True">
...
</CollectionView>
private void CollectionView_RemainingItemsThresholdReached(object sender, EventArgs e)
{
DataCollection.Add(new ViewGroup("group_" + (DataCollection.Count + 1), new ObservableCollection<Page1Model>() {
//items
}));
}
The code work as expected on Android, but not on iOS. Someone faced the issue and has reported it to the github, RSchipper shared a solution to fix the issue. You could refer to the code: https://github.com/xamarin/Xamarin.Forms/issues/8383#issuecomment-578150883
Best Regards,
Jarvan Zhang
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.