Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Pagination in android recyclerview

    • 0
    • 0
    • 0
    • 0
    • 1
    • 0
    • 0
    • 0
    • 12.1k
    Comment on it

     Step 1- Defined here  some necessary variable  

       boolean isLoading=false;    -> this variable will use to show progressbar below to recyclerview in your xml file  when you scroll your recyclerview list .
    
      int mPageSize=10;  -> Number of items you want to fetch from server in a once.
    
          

    Step-2 Set addOnScrollListener to recyclerview

    
    int mTotalItemsCount -> Total item on server side.
    
    recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
                @Override
                public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                    super.onScrollStateChanged(recyclerView, newState);
                }
     
                @Override
                public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                    super.onScrolled(recyclerView, dx, dy);
                    // if dy>0 scrolling down side
                    // if dy<0 scrolling upper side
                    if (dy < 0) {
                        if ((!isLoading && linearLayoutManager.findFirstCompletelyVisibleItemPosition() == 1) && itemsList.size() < mTotalItemsCount) {
                            isLoading = true;
                            
    // this method return pageIndex every time when you hit server to get new data.
                            mPageIndex = returnPageIndex(itemsList.size());
                            loadMoreItems(mPageIndex);
                        }
                    }
                }
            });

    Step-3 Call loadMoreItems method
     

    private void loadMoreItems(int mPageIndex) {
            if (AppUtility.isInternetAvailable(this) == false) {
                AppUtility.showAlertDialog(this, getString(R.string.app_name), getString(R.string.connection_error));
                return;
            }
            // Here will go your server side request code
                    // after get response from server gone visibility to progressbar
                    mLoadMoreProgressbar.setVisibility(View.GONE);
                // add response items to you items list
                  itemsList.add(chatMessageBean);
                 // notify adapter
                 adapter.notifyDataSetChanged();
                // set isLoading to false
                 isLoading = false;
        
        }
    
    

    Step-4 Call returnPageIndex method


    private int returnPageIndex(int sizeOfList) {
            int index = sizeOfList / mPageSize;
            return index;
        }

     

 1 Comment(s)

  • Hi Arvind, i have a rest api in which there are 10 pages and in each page there are 10 videos , i want to do pagination so that when user scrolls then next 10 videos should be fetched from api and page no should change to 0 to 1 and again when user scrolls then page should change from 1 to 2 and so on and next 10 videos should be fetched . PLease help me to do same. I am using retrofit and in my base url i have put pageno as a counter , i am not able to understand where should i increment that counter. Please help me.
Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: