RecyclerView滚动到顶部,而AsyncListDiffer不起作用 [英] RecyclerView scroll to top with AsyncListDiffer not working
问题描述
我正在将RecyclerView
与 AsyncListDiffer
(计算和动画化旧项目与新项目之间的差异,全部在后台线程上.)
I am using RecyclerView
with AsyncListDiffer
(calculates and animates differences between old and new items, all on background thread).
我有一个按钮可以对列表进行排序.在对它进行排序并使用mDiffer.submitList(items);
将其重新设置为RecyclerView
之后,我也调用了recyclerView.scrollToPosition(0)
或(smoothScrollToPosition(0)
),但是它没有任何作用.
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.
我认为这种行为是可以预期的,因为AsyncListDiffer
在调用scrollToPosition(0)
时可能仍在计算差异,因此它没有任何作用.另外,默认情况下AsyncListDiffer
不会滚动到顶部,而是使RecyclerView保持相同的状态.
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.
但是如何在AsyncListDiffer
完成并更新它后告诉RecyclerView
滚动到顶部?
But how do I tell the RecyclerView
to scroll to top after AsyncListDiffer
is done and updates it?
推荐答案
在这里得到答案:
https://stackoverflow.com/a/55264063/1181261
基本上,如果您以不同的顺序提交相同的列表,它将被忽略.因此,首先您需要submit(null)
,然后提交重新排序的列表.
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.
这篇关于RecyclerView滚动到顶部,而AsyncListDiffer不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!