Paginate库的地址:Paginate库的地址
首先在Gradle中进行引入:
compile 'com.github.markomilos:paginate:0.5.1'
Implement Paginate.Callbacks
使用Paginate库的类需要实现Paginate.Callbacks接口,该接口中拥有三个方法,分别是onLoadMore()、isLoading()、hasLoadedAllItems(),源码如下:
public interface Callbacks {
/** Called when next page of data needs to be loaded. */
void onLoadMore();
/**
* Called to check if loading of the next page is currently in progress. While loading is in progress
* {@link com.paginate.Paginate.Callbacks#onLoadMore} won't be called.
*
* @return true if loading is currently in progress, false otherwise.
*/
boolean isLoading();
/**
* Called to check if there is more data (more pages) to load. If there is no more pages to load, {@link
* com.paginate.Paginate.Callbacks#onLoadMore} won't be called and loading row, if used, won't be added.
*
* @return true if all pages has been loaded, false otherwise.
*/
boolean hasLoadedAllItems();
}
可以看出,在需要加载数据的时候会调用onLoadMore()方法、isLoading()则用于判断是否正在进行加载,若正处于加载状态中则返回true,否则返回false、hasLoadedAllItems()则用于判断是否还有需要加载的数据,若已经加载了全部的数据则返回true,否则返回false。
下面来看自己的代码:
@Override
protected void onFinishedCreate() {
super.onFinishedCreate();
recyclerView = findViewById(R.id.recy);
setupPagination();
LoadData();
}
在onFinishedCreate方法中调用了两个方法,一个是setupPagination()、一个是LoadData()。下面来看setupPagination()方法的内容:
private void setupPagination() {
if (paginate != null) {
paginate.unbind