Android RecyclerView adding pagination

前端 未结 3 920
孤独总比滥情好
孤独总比滥情好 2021-01-06 10:33

I created a RecyclerView for my application and trying to find a way to stop huge requests, I want to get the results 10 by 10 with a progress bar. My server will send the r

相关标签:
3条回答
  • 2021-01-06 10:45

    What we did was use ReyclerView.OnScrollListener. Assuming you're using a LinearLayoutManager, you can check the current shown item versus the total count to determine when you've reached the last item, then request the next page. You also need to throw in some additional logic checks to early out to prevent "spamming" as the scroll even happens a lot.

    For example:

    recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            if (!hasMoreContent()) {
                return;
            }
    
            if (currentlyLoadingInitialRequest()) {
                return;
            }
    
            if (alreadyLoadingNextPage()) {
                return;
            }
    
            if (isInErrorState()) {
                return;
            }
    
            LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
            int total = layoutManager.getItemCount();
            int currentLastItem = layoutManger.findLastVisibleItemPosition();
            if (currentLastItem == total - 1) {
                requestNextPage();
            }
        });
    
    0 讨论(0)
  • 2021-01-06 10:53

    You can try this approach:

      mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener()
    {
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            if (dy > 0) //check for scroll down
            {
                visibleItemCount = mLayoutManager.getChildCount();
                totalItemCount = mLayoutManager.getItemCount();
                pastVisibleItems = mLayoutManager.findFirstVisibleItemPosition();
    
                if (loading) {
                    if ((visibleItemCount + pastVisibleItems) >= totalItemCount) {
                        loading = false;
                        if (adapter.countOfShowing < adapter.allChallenges.size()) {
                            Log.e("...", "Last Item Wow !");
                            adapter.increaseCountOfShowing();
                            adapter.notifyDataSetChanged();
                        }
                        loading = true;
                        //Do pagination.. i.e. fetch new data
                    }
                }
            }
        }
    });
    
    0 讨论(0)
  • 2021-01-06 10:58

    https://github.com/kunal-mahajan/PaginationAdeptor

    I have implemented the pagination component / widget and that is very simple to implement. No xml is required only need to extend the class. Example is available in above url with timer task and dummy data. In main activity you may change the number of data available for testing purpose in "final int totalRecords"; If there is any issue will answer your queries. Thanks Kunal

    0 讨论(0)
提交回复
热议问题