簡體   English   中英

RecyclerView在向List添加新項目時移至頂部

[英]RecyclerView moves to top on adding new items to List

我正在嘗試使用recycleler實現無限滾動,但是當我調用notifyDataSetChanged()時,整個列表刷新然后滾動位置返回到頂部。所以我經歷了許多提到的文檔。我正在從上到下滾動在任何聊天應用程序中

所以我檢查layoutManager.findFirstVisibleItemPosition()== 0,然后我下載更多的項目。 每次只下載30個iems。我不希望我的列表移到頂部,它應該保持原樣。

我嘗試使用notifyItemRangeInserted(位置,大小),這給了一些我不想要的動畫。我希望滾動順暢而且無憂無慮。此外,這也不保持位置。如何實現相同

試試這個,為我工作。 您可以將位置保存在變量中。

scrollToPositionWithOffset

我之前已經實現過類似的東西。 願它可以幫助你,隨時問。

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

            }
        });
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM