Android RecyclerView在向列表中添加新项目时移到顶部

Android RecyclerView在向列表中添加新项目时移到顶部,android,android-recyclerview,Android,Android Recyclerview,我试图使用recycler实现一个无休止的滚动,但当我调用notifyDataSetChanged()时,整个列表会刷新,然后滚动位置会回到顶部。因此,我浏览了许多同样提到的文档。我像在任何聊天应用程序中一样从上到下滚动 因此,我检查layoutManager.findFirstVisibleItemPosition()是否=0,然后下载更多项目。 一次只能下载30个IEM。我不想让我的列表移到顶部,它应该保持原样 我尝试使用notifyItemRangeInserted(位置、大小),这提供了

我试图使用recycler实现一个无休止的滚动,但当我调用notifyDataSetChanged()时,整个列表会刷新,然后滚动位置会回到顶部。因此,我浏览了许多同样提到的文档。我像在任何聊天应用程序中一样从上到下滚动

因此,我检查layoutManager.findFirstVisibleItemPosition()是否=0,然后下载更多项目。 一次只能下载30个IEM。我不想让我的列表移到顶部,它应该保持原样


我尝试使用notifyItemRangeInserted(位置、大小),这提供了一些我不想要的动画。我希望滚动平滑且无麻烦。此外,这也不会保持位置。如何实现相同的

尝试一下,对我来说很有用。 可以将位置保存在变量中


我之前已经实现了类似的功能。 愿它能帮助你,尽管问吧

productAdapter = new ProductAdapter(this, products, productsListView);
        productsListView.setAdapter(productAdapter);
            productAdapter.setOnLoadMoreListener(new OnLoadMoreListner() {
            @Override
            public void onLoadMore() {
                products.add(null); // here products is an array list of produt
                productAdapter.notifyItemInserted(products.size() - 1);

                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        //   remove progress item
                        products.remove(products.size() - 1);
                        productAdapter.notifyItemRemoved(products.size());
                        //add items one by one
                        long lastItemId = products.get(products.size() - 1).getId();
                        ArrayList<Product> newProductList = new ArrayList<>(); //download or fetch new data (Some more Products) and assign it newProductList

                        products.addAll(products.size(), newProductList.getProducts());
                        productAdapter.notifyDataSetChanged();
                        productAdapter.setLoaded();
                    }
                }, 2000);

            }
        });
    }
productAdapter=newproductadapter(this,products,productsListView);
productsListView.setAdapter(productAdapter);
productAdapter.setOnLoadMoreListener(新的OnLoadMoreListner(){
@凌驾
public void onLoadMore(){
products.add(null);//这里products是produt的数组列表
productAdapter.notifyItemInserted(products.size()-1);
handler.postDelayed(新的Runnable(){
@凌驾
公开募捐{
//删除进度项目
产品。移除(产品。尺寸()-1);
productAdapter.notifyItemRemoved(products.size());
//逐个添加项目
long lastItemId=products.get(products.size()-1.getId();
ArrayList newProductList=new ArrayList();//下载或获取新数据(一些其他产品)并将其分配给newProductList
products.addAll(products.size(),newProductList.getProducts());
productAdapter.notifyDataSetChanged();
productAdapter.setLoaded();
}
}, 2000);
}
});
}

使用notifyitemInserted,也可以使用recyclerview.scrolltoposition(yourList.size()-1);发布您的代码。@Divyesh我不想让它滚动到那个位置,它会很明显。我根本不想让它滚动。但是滚动不明显,我正在使用它,它工作正常