public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
ViewModel.contactsinfo.CollectionChanged += Contactsinfo_CollectionChanged;
}
private void Contactsinfo_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
Device.BeginInvokeOnMainThread(async () =>
{
int index = listView.DataSource.DisplayItems.Count;
listView.LayoutManager.ScrollToRowIndex(index, true);
});
}
protected override void OnAppearing()
{
ViewModel.GenerateInfo();
listView.ItemsSource = ViewModel.contactsinfo;
base.OnAppearing();
}
} |
Query |
Response |
I've noticed is that the disableAnimation = true in msgList.LayoutManager.ScrollToRowIndex(index, true) does not seem to do anything in this example. When the page loads, the list is populated and it shows the top of the listbox, then animates scrolling down to the bottom of the list in a kind of stutter-y manner. How can I show the list already scrolled to the bottom instead of showing going through the scrolling motions every time? |
If you use AutoFitMode="Height", then scrolling animation will be disabled in Android and IOS platform. Please find the limitations of scrolling with animation,
If your query is different from expecting scrolling with animation apart from this limitations, then we can assist you further with suggestions. |
In your example you have the ViewModel generating the data before base.OnAppearing(). Is this normal? When should I be doing work before OnAppearing() versus after? |
Based on your queries, the workaround suggested may not fit your requirement. Hence we suggest you to wait for the fix which we have promised to deliver on July 16, 2019 and fix changes once delivered will work properly with your initial try of populating items in OnAppearing and perform scrolling in loaded event. |
In your workaround, every time the collection is changed, it will always scroll to the bottom. In my app, this list subscribes to events that occur at random. For example, a delivery or read-receipt may be sent via Firebase or SignalR to the list. In that event, item #10 out of 30 might be changed to show "Message Read". If this happens, the "CollectionChanged" event is triggered and it scrolls the user back to the bottom. What I would actually want is for the list to only scroll to the bottom in the event the whole collection is replaced/reset, or when a new item is added. Not when other aesthetic changes occur.
|
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}
protected override void OnAppearing()
{
ViewModel.GenerateInfo();
listView.ItemsSource = ViewModel.contactsinfo;
Device.BeginInvokeOnMainThread(async() =>
{
int index = listView.DataSource.DisplayItems.Count;
listView.LayoutManager.ScrollToRowIndex(index, true);
});
base.OnAppearing();
}
} |