github主页https://github.com/scwang90/SmartRefreshLayout
添加依赖
implementation 'io.github.scwang90:refresh-layout-kernel:2.0.5' //核心必须依赖
implementation 'io.github.scwang90:refresh-header-classics:2.0.5' //经典刷新头
implementation 'io.github.scwang90:refresh-footer-classics:2.0.5' //经典加载
刷新用法:
1.有刷新头/尾
2.刷新监听
3.结束刷新
4.自动刷新(显示刷新动画并且触发刷新事件)
xml:
<com.scwang.smart.refresh.layout.SmartRefreshLayout
android:id="@+id/smartRefreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />
</com.scwang.smart.refresh.layout.SmartRefreshLayout>
activity:
刷新头,刷新尾
binding.smartRefreshLayout.setRefreshHeader(new ClassicsHeader(getContext()));
binding.smartRefreshLayout.setRefreshFooter(new ClassicsFooter(getContext()));
自动刷新
binding.smartRefreshLayout.autoRefresh();
刷新监听
binding.smartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
@Override
public void onRefresh(@NonNull RefreshLayout refreshLayout) {
//刷新:上拉,展示第一页的内容,不管请求成功与否,都结束刷新
vm.Refresh(categoryId);
}
});
binding.smartRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
@Override
public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
vm.lodeMore(categoryId);
}
});
结束刷新
if (aBoolean){
//刷新为true,这里结束刷新
binding.smartRefreshLayout.finishRefresh();
}else {
binding.smartRefreshLayout.finishLoadMore();
}