I am using RecyclerView
with AsyncListDiffer
(calculates and animates differences between old and new items, all on background thread).
I have a button to sort the list. After I sort it and re-set it to RecyclerView
using mDiffer.submitList(items);
I also call recyclerView.scrollToPosition(0)
or (smoothScrollToPosition(0)
), but it has no effect.
I think this behaviour is expected, as AsyncListDiffer
is probably still calculating differences at the time that scrollToPosition(0)
is called, so it has no effect. Additionally, by default AsyncListDiffer
does not scroll back to top, but instead it keeps RecyclerView in the same state.
But how do I tell the RecyclerView
to scroll to top after AsyncListDiffer
is done and updates it?
This got answered here:
https://stackoverflow.com/a/55264063/1181261
Basically, if you submit the same list with different order, it will be ignored. So first you need to submit(null)
and then submit your re-ordered list.
I am concerned that while .submitList(null)
may have worked for you, it only refreshed your entire RecyclerView without rendering the desired animated list updates.
Solution is to implement the .submitList( List<T> list)
method inside your ListAdapter as follows:
public void submitList(@Nullable List<T> list) {
mDiffer.submitList(list != null ? new ArrayList<>(list) : null);
}
This way you allow the ListAdapter to retain its currentList and have it "diffed" with the newList, thereby the animated updates, as opposed to "diffing" with a null
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With