项目需求
在页面数据中,每次只显示一部分,当手指操作从下往上滑动时,进行请求加载更多数据,当从上往下滑动时,进行整个所有数据的刷新。
需求实现
1.引入依赖
//刷新
implementation 'com.scwang.smart:refresh-layout-kernel:2.0.1' //核心必须依赖
implementation 'com.scwang.smart:refresh-header-classics:2.0.1' //经典刷新头
implementation 'com.scwang.smart:refresh-header-radar:2.0.1' //雷达刷新头
implementation 'com.scwang.smart:refresh-header-falsify:2.0.1' //虚拟刷新头
implementation 'com.scwang.smart:refresh-header-material:2.0.1' //谷歌刷新头
implementation 'com.scwang.smart:refresh-header-two-level:2.0.1' //二级刷新头
implementation 'com.scwang.smart:refresh-footer-ball:2.0.1' //球脉冲加载
implementation 'com.scwang.smart:refresh-footer-classics:2.0.1' //经典加载
2.界面布局,使用SmartRefreshLayout包裹我们显示数据的RecyclerView
<com.scwang.smart.refresh.layout.SmartRefreshLayout
android:id="@+id/sr_home"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/ll_home"
android:layout_marginStart="20dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="20dp">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_home"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:overScrollMode="never"
android:scrollbars="none" />
</com.scwang.smart.refresh.layout.SmartRefreshLayout>
3.快速实现
binding.srHome.apply {
//设置头部刷新效果
setRefreshHeader(MaterialHeader(context))
//设置底部刷新效果
setRefreshFooter(ClassicsFooter(context))
setEnableLoadMore(true)
setEnableRefresh(true)
//上拉加载更多
setOnLoadMoreListener {
it.finishLoadMore(2000)
}
//下拉刷新
setOnRefreshListener {
it.finishRefresh(2000)
}
}
OVER