What is the safest way to add new messages while keeping the scroll position stable, especially when inserting at the top?

Platform: .NET MAUI| Category: Collection View

Capture the first visible item (or its index) and offset. Insert new messages using a batch update, then restore scroll position using ScrollTo(savedItem/index, ScrollToPosition.Start, animate: false). This preserves the viewport and avoids jumping.

// before insert
var firstVisibleIndex = args.FirstVisibleItemIndex; // from ItemsViewScrolled
var firstVisibleItem = view.ItemsSource.Cast<object>().ElementAt(firstVisibleIndex);

// insert batch at index 0
items.InsertRange(0, newItems);

// restore
collectionView.ScrollTo(firstVisibleItem, position: ScrollToPosition.Start, animate: false);

Share with