简体   繁体   English

如何在RecyclerView中实现更多负载? 我清单上有500件物品

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

How to implement load more in recycler view ? 如何在回收站视图中实现更多负载? I am getting 500 items in list but I want to restrict to 20 items. 我列表中有500个物品,但我想限制为20个物品。 On load more with progresbar I want to show next 20 items... goes on flow. 使用progresbar加载更多时,我想显示下20个项目...继续进行。

In my Activity, I'm getting products adding to list 在我的活动中,我正在将产品添加到列表中

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

On Load more listiner 加载更多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);

        }
    });


}

In Adapter 在适配器中

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

//It is getting crash here when it reaches the condition means total-1 = lastvisibleitemcount =20// //达到条件时这里会崩溃,意味着total-1 = lastvisibleitemcount = 20 //

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

Please Help. 请帮忙。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将 controller 中的 RequestBody 定义为列表,我收到 500 错误 - How to define RequestBody in controller as list, I am getting 500 error RecyclerView问题加载了n个以上的项目 - RecyclerView problem to load more than n items android recyclerview json加载更多项目 - android recyclerview json load more items 如何使用 Firebase 数据库中的数据在 RecyclerView 上加载更多分页(无限滚动) - How to implement load more Pagination(infinite scrolling) on RecyclerView using data from Firebase database 从recyclerview中的editText搜索,我在其中使用改型从API获取项目 - search from editText in recyclerview where i am getting items from API using retrofit 如何使用 RecyclerView 逐步加载带有 RecyclerView 的屏幕滚动屏幕? - How do I scroll the screen with the RecyclerView inside with RecyclerView load items step by step? 尝试将JSON数据加载到RecyclerView时为什么会出现NullPointerException? - Why am I getting NullPointerException when trying to load JSON data into RecyclerView? 为什么会收到http 500响应? - Why am I getting a http 500 response? 如何一次在recyclerView中加载所有项目? - How to load all items in recyclerView at once? 如何在Recyclerview中加载更多JSON数据 - How to load more JSON data in Recyclerview
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM