Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement pagination in RecyclerView on scroll

Recyclerview comes with its own scroll listener which has the following methods :

void onScrollStateChanged(RecyclerView recyclerView, int newState)

Callback method to be invoked when RecyclerView's scroll state changes.

void onScrolled(RecyclerView recyclerView, int dx, int dy)

Callback method to be invoked when the RecyclerView has been scrolled.

Is there any way to trigger loader to load more data when scroll reaches end of the list?

I have implemented this way:

@Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
    GenerItem generItem=generItems.get(i);
    Log.d("TAG","position "+i);
    if(i==generItems.size()-1)
    ((GenerSearchActivity)mContext).onScroll(i);
    viewHolder.bindValues(generItem);
}

Here onScroll() in Activity, will trigger the loader to load more data. What is the best way please suggest.

like image 389
RQube Avatar asked Dec 03 '25 18:12

RQube


1 Answers

Make Next Call at the end of scroll

enter image description here

There are essentially 3 steps.

  1. Notify when the list is scrolled
  2. Make REST call (for NEXT page)
  3. Add the result in the old list + Notify DataSet change

CALLBACK

But first, we need a Callback which would work as a bridge between RecyclerView.Adapter and Activity

public interface PaginationCallBack{
     public void oadNextPage();
}

Implement this callback in Your Activity

class YourActivity extends AppCompatActivity implements PaginationCallBack{

     int pageNum = 1;

     @Override
     public void loadNextPage(){
           // Your implementation
     }
}

Initialize Callback in RecyclerView.Adapter

class YourAdapter extends RecyclerView.Adapter{

     private PaginationCallBack paginationCallBack;

     public YourAdapter(PaginationCallBack paginationCallBack) {
        this.paginationCallBack = paginationCallBack;
     }

}

STEP 1 Add a condition in onBindViewHolder method and notify with a Callback.

@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewholder, int position) {

    if(position+1==images.size()){
        paginationCallBack.loadNextPage();  // Callback
    }

}

Step 2: Call NEXT Page

getImages(++pageNum) // YOUR REST method which page number

@Override
public void loadNextPage(){
    getImages(++pageNumber) // REST call with next Page 
}

Step 3 Add the result in old list and notify datasetChanged

public void getImages(int pageNum){

      List<Images> newResults = //RESTCALL
      imageList.addAll(newResults);
      adapter.updateDataSet(imageList)

}

Where is updateDataSet(imageList) method?

Write this method inside RecyclerView.Adapter

 public void updateDataSet(List<GalleryMedia> newImages){

    if(newImages!=null){
        images = newImages;
    }

    notifyDataSetChanged();
}

Full Code

RecyclerView Pagination

Result:

enter image description here

like image 100
Rohit Singh Avatar answered Dec 06 '25 08:12

Rohit Singh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!