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
andStaggeredGridLayoutManager
.
repositories {
google()
jcenter()
}
dependencies {
implementation "com.hendraanggrian.recyclerview:recyclerview-paginated:$version"
}
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" />
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();
}
}
}
recyclerView.setLayoutManager(lm)
recyclerView.setAdapter(adapter)
recyclerView.setPagination(pagination)
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);