簡體   English   中英

如何在RecyclerView中實現更多負載? 我清單上有500件物品

[英]How to implement load more in a RecyclerView? I am getting 500 items in list

如何在回收站視圖中實現更多負載? 我列表中有500個物品,但我想限制為20個物品。 使用progresbar加載更多時,我想顯示下20個項目...繼續進行。

在我的活動中,我正在將產品添加到列表中

 Call<NextUrl> call = api.getSubCatogeryProducts(url);
    call.enqueue(new Callback<NextUrl>() {
        @Override
        public void onResponse(Call call, Response response) {
            progressBar.setVisibility(View.VISIBLE);
            NextUrl responseData = (NextUrl) response.body();
            String displayResponse;
            if (response.isSuccessful()) {

                displayResponse = "";

                for (int j = 0; j <20; j++) {
                     productAttributes = responseData.getProductInfoList().get(j).getProductBaseInfo().getProductAttributes();
                    sendingproducts.add(productAttributes);

                }

                mDataAdapter = new DataAdapter(sendingproducts, recyclerView);

                progressBar.setVisibility(View.GONE);
                recyclerView.setAdapter(mDataAdapter);
                Toast.makeText(TestActivity.this, "loading data", Toast.LENGTH_SHORT).show();

            } else {
                progressBar.setVisibility(View.GONE);

                Toast.makeText(TestActivity.this, "Un Successful", Toast.LENGTH_SHORT).show();

            }
            progressBar.setVisibility(View.GONE);

        }


        @Override
        public void onFailure(Call call, Throwable t) {
            progressBar.setVisibility(View.GONE);

            Toast.makeText(DashboardActivity.context, " failed to get the data", Toast.LENGTH_LONG).show();
        }
    });
}

加載更多listiner

 mDataAdapter.setOnLoadMoreListener(new OnLoadMoreListner() {
        @Override
        public void onLoadMore() {
            //add null , so the adapter will check view_type and show progress bar at bottom
            sendingproducts.add(null);
            mDataAdapter.notifyItemInserted(sendingproducts.size() - 1);

            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    //   remove progress item
                    sendingproducts.remove(sendingproducts.size() - 1);
                    mDataAdapter.notifyItemRemoved(sendingproducts.size());
                    //add items one by one
                    int start = sendingproducts.size();
                    int end = start + 20;

                    if(end<=sendingproducts.size()){
                        sendingproducts.addAll(sendingproducts.subList(start,end));
                        mAdapter.notifyItemInserted(sendingproducts.size());

                    }
                    mDataAdapter.setLoaded();
                    //or you can add all at once but do not forget to call mAdapter.notifyDataSetChanged();
                }
            }, 2000);

        }
    });


}

在適配器中

 recyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);

                int total = linearLayoutManager.getItemCount();
                int firstVisibleItemCount = linearLayoutManager.findFirstVisibleItemPosition();
                int lastVisibleItemCount = linearLayoutManager.findLastVisibleItemPosition();

                //to avoid multiple calls to loadMore() method
                //maintain a boolean value (isLoading). if loadMore() task started set to true and completes set to false
                if (!loading) {
                    if (total > 0)
                        if ((total - 1) == lastVisibleItemCount) {

//達到條件時這里會崩潰,意味着total-1 = lastvisibleitemcount = 20 //

                            onLoadMoreListener.onLoadMore();
                        } else
                            Log.e("ok","okokokokokokoko");
                }
            }

請幫忙。

暫無
暫無

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

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