简体   繁体   English

如何在不滚动到顶部的情况下将新项目添加到recyclerview

[英]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.

相关问题 当我向下滚动时,滚动以在recyclerview中添加项目总是自动显示在顶部 - on scrolled to add items in recyclerview always comes to top automatically when i scroll down 如何使用 RecyclerView 创建滚动返回顶部按钮 - How To Create a Scroll Back To Top Button with RecyclerView 如何在Android中向recyclerView添加项目 - How to add items to recyclerView in android 如何在顶部显示特定的recyclerview项目? - How do show specific recyclerview items at the top? Android recyclerview 滚动到顶部 - Android recyclerview scroll to top 如何在垂直回收站视图上添加水平滚动? - How to add horizontal scroll on vertical recyclerview? 滚动到回收站视图中下一个项目的顶部 - Scroll to top of next item in a recyclerview 在 Recyclerview 中,如何将初始滚动位置更改为第一项的顶部? - In Recyclerview, how to change initial scroll position to the top of first item? LinearLayout 和 NestedScrollView 中的 RecyclerView - 如何滚动到某个位置的项目顶部? - RecyclerView inside LinearLayout and NestedScrollView - How to scroll to top of item on a certain position? 如何在RecyclerView中执行从顶部到某个位置的滚动项目? - How in RecyclerView perform scroll item with some position to top?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM