So I am trying to set up an infinitely scrolling listview that's utilizing the recycled caching strategy. I actually see a huge improvement from the older listview, where, after a few hundred or so images it lags. Now, I can load in 100, 200 images and scrolling is on point. So good job. What I am a little lost in is the correct way to listen at any given point (cell) whether or not to load more data. My old listview, which was platform specific, utilized the GetCell method and I was able to give it some heuristics to determine when to load more cells. And from my understanding, recycled listviews are typically binded to a OnBindingContextChange
method, which I have and is working. As such:
var item = BindingContext as XamarinMobile.ViewModels.GridCellViewModel;
But I am seeing some weird issues. I was able to give each cell a cell index and pass it to my view model for the cell. When the cell being viewed is #6 or higher (we load about 20 at a time) I want to load more data. As such:
if(item.cellIndex >= ((item.numberOfStories - RowFetchCount) + 6)) { item.FetchMoreData(); }
So far this works somewhat good, I do see some weird issues but nothing pertaining to why I am asking this question. The issue I am having now is how to effectively change the listview item source without any UI performance dip. Essentially, what's the correct way to do the following:
(this piece of code gets hit after the FetchMoreData
method is called and this is when we have data ready to be added to collection)
foreach (MobileObjects.GridListStory story in stories.StoryList)
{
index++;
if (index > stories.StoryList.Count - RowFetchCount)
{
if (index % 4 == 0)
{
cells.Add(new ViewModels.GridCellViewModel { Image = string.Empty, Headline = "ADVERTISEMENT", ActivityIndicator = new Xamarin.Forms.ActivityIndicator(), IsAd = true, cellIndex = index, numberOfStories = stories.StoryList.Count, LoadPageDate = LoadPageDate });
}
else
{
cells.Add(new ViewModels.GridCellViewModel { Image = story.SquareImageURL, Headline = story.Headline, ActivityIndicator = new Xamarin.Forms.ActivityIndicator(), IsAd = false, cellIndex = _index, numberOfStories = stories.StoryList.Count, FetchMoreData = InitMoreData, LoadPageDate = LoadPageDate });
}
}
if (index % 4 != 0)
{
_index++;
}
}
This causes the listview to lag a bit. Not a lot but I feel like I am doing something wrong? Here is the declaration of cells
public System.Collections.ObjectModel.ObservableCollection<ViewModels.GridCellViewModel> cells { get; set; }
So 1) Am I already doing this correctly?
2) When I try to do cells = [dataObject]
the listview item source doesn't change, it seems like I have to actually add them using the method above. Is there a cleaner way?