Search code examples
androidandroid-recyclerviewandroid-asynclistdifferandroid-diffutils

RecyclerView scroll to top with AsyncListDiffer not working


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?


Solution

  • 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.