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();
}
adapter = new RecyclerAdapter<BindRecordVo.ContentBean>(R.layout.item_bind_record) {
@Override
protected void convert(AdapterHelper helper, BindRecordVo.ContentBean item, int position) {
helper.setText(R.id.device_name, item.alias);
helper.setText(R.id.groupFullName, item.groupFullName);
}
};
recyclerView.setLayoutManager(new LinearLayoutManager(context));
recyclerView.setAdapter(adapter);
paginate = Paginate.with(recyclerView, this)
.setLoadingTriggerThreshold(1)
.addLoadingListItem(addLoadingRow)//添加加载的列表项
.build();
}
首先对paginate对象的状态进行判断,如果paginate的状态不为空,则将paginate对象与RecyclerView进行拆分,然后是一个RecyclerView的adapter对象的声明,R.layout.item_bind_record 表示每一个view的容器,convert()方法由RecyclerAdapter类中的onBindViewHolder()方法提供,该方法在类中的描述如下:
/**
* Implement this method and use the helper to adapt the view to the given item.
*
* @param helper A fully initialized helper.
* @param item The item that needs to be displayed.
* @param position
*/
protected abstract void convert(AdapterHelper helper, T item, int position);
通过使用helper对象将给出的item适配到view中,然后recyclerView设置LayoutManager和Adapter,最后在recyclerView上创建paginate对象。
下面是LoadData()方法中的内容:
private void LoadData() {
JkAPiClientUti.getApi().getOrgBindRecords(page, 3, 2)
.compose(ComposeHelper.response(new IProgressImpl(context), new OnToastDataFail()))
.compose(RxLife.with(this).bindToLifecycle())
.subscribe(new Action1<BindRecordVo>() {
@Override
public void call(BindRecordVo bindRecordVo) {
int total = bindRecordVo.total;
int size = bindRecordVo.pageable.size;
totalPages = (total / size);
page = bindRecordVo.pageable.page;
Finelog.d(bindRecordVo);
Finelog.d(totalPages);
ArrayList<BindRecordVo.ContentBean> arrayList = new ArrayList<>();
for (int i = 0; i < bindRecordVo.content.size(); i++) {
String alias = bindRecordVo.content.get(i).alias;
String groupFullName = bindRecordVo.content.get(i).groupFullName;
arrayList.add(new BindRecordVo.ContentBean(alias, groupFullName));
}
Finelog.d("page=" + page);
adapter.addData(arrayList);
adapter.notifyDataSetChanged();
Finelog.d(adapter.getData());
loading = false;
}
}, new Action1<Throwable>() {
@Override
public void call(Throwable throwable) {
loading = false;
}
});
}
在LoadData()方法中通过访问网络接口去请求数据,将请求到数据保存在bindRecordVo对象中,通过total和size两个变量来计算分页的总页数totalPages,然后使用for循环进行遍历,将数据添加到arrayList对象中。通过adapter对象将arrayList添加到recyclerView中,在请求完成之后将Loading的值赋为false,来控制加载方法的调用。
接下来是CallBacks接口中的三个方法的具体实现:
@Override
public void onLoadMore() {
if (!loading) {
page++;
loading = true;
LoadData();
}
}
@Override
public boolean isLoading() {
return loading;
}
@Override
public boolean hasLoadedAllItems() {
return page == totalPages;
}
在onLoadMore()方法中首先对loading的值进行判断,当loading不为true的时候将loading的值赋为true,进行数据的加载,其中page表示当前请求数据的页数,调用LoadData方法加载数据。
在isLoading()方法中返回当前loading的值,若loading为false,表示当前不在加载数据状态,若loading为true,则表示当前正在加载数据。
在hasLoadedAllItems()方法中对page和totalPages的值进行比较,其中page的值会在每次加载数据时自增一次,代表当前请求的页数,而totalPages的值则是在请求数据的过程中计算所得,表示总页数。当当前请求的页数等于总页数时,表示已经加载到了最后一页,全部数据都已经加载完毕。当此方法返回值为true的时候,当前paginate对象的任务已经全部做完,之后此paginate对象不会再做任何操作。