Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 75885

Removing "oldest" item form Listview dataSource make it scroll to the bottom

$
0
0

Hi,

I want to make an "infinit listveiw" by adding and removing items from the itemsource as the user scroll the list.
Adding items when the user scroll the bottom of the list works fine using ItemAppearing event, but for performance I want to remove oldest Items from the "top" of the datasource when it reachs let's say 40 items, for instance there is 20 older items (on the top, not visible) 10 visible items and 10 newer items on the bottom (Not visible). when I load the 10 newer items, I want to remove the 10 oldest

The problem is that when I remove non visible items form the ObservableCollection the ListView automatically scroll to the bottom, witch trigger the load of newest item and so one...

Here is the code (The Page) :

void traceList_ItemAppearing(object sender, ItemVisibilityEventArgs e)
{
   MyFlightRecMenuViewModel vm = ((MyFlightRecMenuViewModel)this.BindingContext);
   int itemId = ((Trace)e.Item).Id;
   if(itemId == vm.lastTraceId)
   {
        Debug.WriteLine("List end First = " + vm.firstTraceId + " Last = " + vm.lastTraceId);
        ((MyFlightRecMenuViewModel)this.BindingContext).LoadNextTraceCommand.Execute(null);     
   }
}

The viewModel this is the function called in the LoadNextTraceCommand:

private async Task LoadNextTrace()
        {
            if (CurrentPageIndex >= MaxPages )
                   return;
            CurrentPageIndex++;
            //Get the data
            ICollection<Trace> list = await traceSvc.GetLastTraceAsync(CurrentPageIndex, ItemPerPage);
           //Supresse ObservableCollection notification while we changing the list            
           UserLastTracks.SuppressNotification = true;
           //Add the new items at the bottom
            UserLastTracks.AddRange(list);
           //If we reached the max items allowed in the data source remove the oldest
            if ((UserLastTracks.Count) > MaxTraceOnScreen)
            {
                for (int i = 0; i < ItemPerPage; i++)
                    UserLastTracks.RemoveAt(0);
            }
            //Enable the notifications
            UserLastTracks.SuppressNotification = false;
            firstTraceId = UserLastTracks.First().Id;
            lastTraceId = UserLastTracks.Last().Id;
            //Notify the changes
            NotifyPropertyChanged(() => UserLastTracks);
        }

Any hints are welcome...

Thx


Viewing all articles
Browse latest Browse all 75885

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>