[英]how to add new items to recyclerview without scroll to top
I want to set new items to top of recyclerview. 我想将新项目设置为recyclerview的顶部。 But when I set items, recyclerview scroll to top automatically.
但是当我设置项目时,recyclerview会自动滚动到顶部。
I tried many codes but I can't resolve that. 我尝试了很多代码,但我无法解决这个问题。
here is my addItem
method: 这是我的
addItem
方法:
public void addItem(final ArrayList<String> list) {
for (int i=0; i< list.size();i++){
items.add(i,list.get(i));
}
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
// notifyDataSetChanged();
// notifyItemRangeInserted( 0,list.size() );
// notifyItemInserted();
notifyItemRangeInserted(10,11);
// notifyDataSetChanged();
}
});
}
I tried all of commented method. 我尝试了所有评论方法。
could you please help me to resolve this?? 你能帮我解决一下吗?
Thanks 谢谢
Finaly I got a solution. 最后我得到了一个解决方案。 I put
notifyItemRangeChanged
after set new Items. 我在设置新项目后放置了
notifyItemRangeChanged
。 my solution is: 我的解决方案是:
public void addItem(final ArrayList<String> list,int h) {
final int oldsize = items.size();
for (int i=list.size()-1; i>=0 ;i--){
items.add(0,list.get(i));
}
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
notifyItemRangeInserted(0,items.size()-oldsize);
}
});
}
I hope this is been useful for everyone. 我希望这对每个人都有用。 Thanks
谢谢
You can use recyclerView.scrollToPosition(pos);
你可以使用
recyclerView.scrollToPosition(pos);
where pos is position where you want to focus your listview at after some operation on list. 其中pos是您希望在列表上的某些操作之后将列表视图聚焦的位置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.