Skip to content
This repository has been archived by the owner on Aug 23, 2023. It is now read-only.

hanggrian/recyclerview-paginated

Repository files navigation

download build license

Paginated RecyclerView

demo_list demo_grid

Customizable pagination in Android RecyclerView. Some of the features are:

  • Placeholder view when page is being fetched.
  • Error view when page has failed to load.
  • Works with LinearLayoutManager and StaggeredGridLayoutManager.

Download

repositories {
    google()
    jcenter()
}

dependencies {
    implementation "com.hendraanggrian.recyclerview:recyclerview-paginated:$version"
}

Usage

Use PaginatedRecyclerView

It has several attributes:

  • paginationThreshold - set the offset from the end of the list at which the paginate more event needs to be triggered, default is 5.
  • placeholderAdapter - class name of customized placeholder adapter, may be ignored.
  • errorAdapter - class name of customized error adapter, may be ignored.
<com.hendraanggrian.recyclerview.widget.PaginatedRecyclerView
    android:id="@+id/recyclerView"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

Create Pagination

public class PostPagination extends PaginatedRecyclerView.Pagination {
    @Override
    public boolean getPageStart(int page) {
        return 0;
    }

    @Override
    public void onNextPage(int page) {
        if (loadItemSuccess) {
            populateItems(); // add items to adapter
            notifyLoadingCompleted();
        }
        if (reachPageEnd) {
            notifyPaginationFinished();
        }
    }
}

Attach Pagination to PaginatedRecyclerView

recyclerView.setLayoutManager(lm)
recyclerView.setAdapter(adapter)
recyclerView.setPagination(pagination)

Customization

Use custom loading row

Create custom loading adapter, and supply it to PaginatedRecyclerView.

public class CustomLoadingAdapter extends LoadingAdapter {

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ...
    }
}

CustomLoadingAdapter placeholderAdapter = new CustomLoadingAdapter();
recyclerView.setLoadingAdapter(placeholderAdapter);