https://github.com/scwang90/SmartRefreshLayout
RecyclerView+SmartRefreshLayout 实现刷新和加载更多
//1.1.0 (1.0.5及以前版本的老用户升级需谨慎,API改动过大)
compile 'com.scwang.smartrefresh:SmartRefreshLayout:1.1.0-alpha-31'
compile 'com.scwang.smartrefresh:SmartRefreshHeader:1.1.0-alpha-31'//没有使用特殊Header,可以不加这行
compile 'com.android.support:appcompat-v7:25.3.1'//版本 23以上(必须)
xml
<com.scwang.smartrefresh.layout.SmartRefreshLayout
android:id="@+id/smartRefreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.scwang.smartrefresh.layout.header.ClassicsHeader
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.scwang.smartrefresh.layout.footer.ClassicsFooter
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srlClassicsSpinnerStyle="Translate" />
</com.scwang.smartrefresh.layout.SmartRefreshLayout>
activity
private void getBookBg() {
getData();
smartRefreshLayout
.setOnLoadMoreListener(new OnLoadMoreListener() {
@Override
public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
page++;
getData();
refreshLayout.finishLoadMore(500);
}
})
.setOnRefreshListener(new OnRefreshListener() {
@Override
public void onRefresh(@NonNull RefreshLayout refreshLayout) {
page = 1;
getData();
refreshLayout.finishRefresh(500);
}
});
}
BookBgOnlineAdapter adapter;
private void getData() {
HttpRequest2.getBookBg(page).subscribe(new ApiSubcriber<ListResult<BookBackground>>() {
@Override
public void onResult(ListResult<BookBackground> result) {
LogUtils.e(result.list);
List<BookBackground> list = result.list;
if (list == null || list.size() == 0) {
return;
}
if (adapter == null || page == 1) {
adapter = new BookBgOnlineAdapter();
adapter.addData(result.list);
//
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new GridLayoutManager(activity, 2, GridLayoutManager.VERTICAL, false));
} else {
adapter.addData(result.list);
adapter.notifyDataSetChanged();
}
}
@Override
public void onFailed(Result result) {
Log.e(TAG, "onFailed: \n" + result.message);
}
@Override
public void onCompleted() {
super.onCompleted();
}
});
}