RecyclerView notifyDataSetChanged滚动到顶部 [英] RecyclerView notifyDataSetChanged scrolls to top position
问题描述
在RecyclerView
上调用notifyDataSetChanged
时,它不保留滚动位置并滚动到顶部,有什么解决方法可以保留其滚动位置?
when calling notifyDataSetChanged
on RecyclerView
it doesn't retain the scroll position and scrolls to top, is there any solution to retain it's scroll position?
推荐答案
如果您的RecyclerView列表项的父项具有"wrap_content"属性,则Recyclerview会再次计算高度并滚动到顶部.
If your RecyclerView list item parent has "wrap_content" property, Recyclerview calculates the heights again and scrolls top.
有两种解决方案:
- 将身高设置为恒定值,例如:
layout_height="100dp"
-
像这样使用StaggeredGridLayoutManager:
- Set your height a constant value like this:
layout_height="100dp"
Use StaggeredGridLayoutManager like this:
mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL));
第二种解决方案效率更高,我建议采用这种解决方案
The second solution is more efficient and I suggest this one
这篇关于RecyclerView notifyDataSetChanged滚动到顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!