How can auto-scrolling to the latest message be implemented only when the user is already near the bottom?

Platform: .NET MAUI| Category: Collection View

Track the last visible index using ItemsViewScrolled. Auto-scrolling only when the user is within a certain threshold of the bottom ( e.g., the last two or three items prevents the interruption of active scrolling.

void OnItemsViewScrolled(object s, ItemsViewScrolledEventArgs e)
{
  var lastIndex = Items.Count - 1;
  bool nearBottom = (lastIndex - e.LastVisibleItemIndex) <= 2;
  if (nearBottom) collectionView.ScrollTo(Items.Last(), position: ScrollToPosition.End, animate: true);
}

Share with