如何保持在一个RecyclerView滚动位置在其第一个索引和呼叫notifydatasetchange添加项目后 [英] How to remain at a scroll position in RecyclerView after adding items at its first index and call notifydatasetchange
本文介绍了如何保持在一个RecyclerView滚动位置在其第一个索引和呼叫notifydatasetchange添加项目后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用的是RecyclerView,我的第一个项目之前添加项目,滚动位置向上移动到新的第一项补充道。我该如何维护我的滚动位置的第一索引添加新项目后,并调用 notifydatasetchange()
I am using a RecyclerView, I add items before the first item, the scroll position moves up to the newly first item added. How can I maintain my scroll position after adding new items at its first index and call notifydatasetchange()
?
这就是我在做的适配器
mCurrentFragment.items.addAll(0, createLineItems(dataArrayList));
notifyDataSetChanged();
有什么建议?
推荐答案
有两种选择:
-
使用的更复杂的版本
通知
Use the more sophisticated versions of
notify
List newData = createLineItems(dataArrayList);
mCurrentFragment.items.addAll(0, newData);
notifyItemRangeInserted(0, newData.size());
使用稳定的ID 即可。在您的适配器覆盖众长getItemId(INT位置)
,使其返回有意义值并调用 setHasStableIds(真);
就可以了。
Use stable IDs. On your adapter override public long getItemId (int position)
to make it return MEANINGFUL values and call setHasStableIds(true);
on it.
这篇关于如何保持在一个RecyclerView滚动位置在其第一个索引和呼叫notifydatasetchange添加项目后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文