RecyclerView scroll to top with AsyncListDiffer not working

a 夏天 提交于 2020-01-04 01:53:04

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/55262409/recyclerview-scroll-to-top-with-asynclistdiffer-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!