【Android】上拉加载更多,下拉刷新数据快速实现

项目需求

在页面数据中,每次只显示一部分,当手指操作从下往上滑动时,进行请求加载更多数据,当从上往下滑动时,进行整个所有数据的刷新。

需求实现

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

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值