【问题标题】:RecyclerView moves to top on adding new items to ListRecyclerView 在将新项目添加到列表时移至顶部
【发布时间】:2016-11-14 08:28:48
【问题描述】:

我正在尝试使用回收器实现无限滚动,但是当我调用 notifyDataSetChanged() 时,整个列表会刷新,然后滚动位置会回到顶部。所以我浏览了许多相同的文档。我从顶部滚动就像在任何聊天应用程序中一样。

所以我检查 layoutManager.findFirstVisibleItemPosition() == 0,然后我下载更多项目。 一次只下载 30 个 iem。我不希望我的列表移到顶部,它应该保持在原来的位置。

我尝试使用 notifyItemRangeInserted(position,size),这给出了一些我不想要的动画。我希望滚动流畅且无忧无虑。这也不能保持位置。如何实现相同

【问题讨论】:

  • 使用 notifyitemInserted 并使用 recyclerview.scrolltoposition(yourList.size() -1);
  • 发布您的代码。
  • @Divyesh 我不想让它滚动到那个位置,它会很明显。我根本不想让它滚动。
  • 但滚动并不明显,我目前正在使用它并且效果很好

标签: android android-recyclerview


【解决方案1】:

试试这个,对我有用。 您可以将位置保存在变量中。

scrollToPositionWithOffset

【讨论】:

    【解决方案2】:

    我之前已经实现了类似的东西。 希望对你有帮助,欢迎提问。

    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);
    
                }
            });
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-11
      • 2016-12-15
      • 1970-01-01
      • 2019-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多