Question

I'm using an MvxListView that is bound to an observablecollection to display chat messages. I found a way that the listview always scrolls down to the bottom with these 2 attributes:

android:stackFromBottom="true"
android:transcriptMode="alwaysScroll"

This works great when I add a message to the observablecollection via a command (when I send a message myself), but when I receive a message (via signalr) via the viewmodel and then add the message exactly the same way to the observablecollection, the ListView is updated but it doesn't scroll down. When I scroll down manually the message is there, it just doesn't scroll down automatically. I'm thinking this might have something to do with the message arriving via signalr, because it's an async callback.

I tried to manually force the listview to scroll down in an MvxAdapter like this:

   public override void NotifyDataSetChanged()
    {
        base.NotifyDataSetChanged();
        ((FragmentActivity)_context).RunOnUiThread(() =>
        {
            _list.SetSelection(this.Count-1);
        });
    }

and then call that explicitly when a message arrives but that doesn't do anything either.

Help?

Was it helpful?

Solution

Just saw this answer from Slodge:

Advice on where and when to use ObservableCollection in MvvmCross

The UI updates must be done synchronously. And thinking about it, it makes sense indeed. The answer for me was to use:

MvxMainThreadDispatcher.Instance.RequestMainThreadAction(() => {
    // fill your collection in the viewmodel and do a propertychanged
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top