android加载更多、下拉刷新第三方框架:SmartRefreshLayout,初始化只要2句代码,可自定义加载更多、下拉刷新的样式

效果:

在这里插入图片描述在这里插入图片描述

1、依赖:


  implementation 'com.scwang.smartrefresh:SmartRefreshLayout:1.1.0-alpha-23'
  compile 'com.scwang.smartrefresh:SmartRefreshHeader:1.1.0-alpha-23'//没有使用特殊Header,可以不加这行
  implementation 'com.android.support:recyclerview-v7:28.0.0'//recycleview依赖

2、xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <com.scwang.smartrefresh.layout.SmartRefreshLayout
        android:id="@+id/url_imgs_refreshlayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
		这里项目需求不需要刷新,就设置为1,设置为0会无效的
        <TextView
            android:layout_width="match_parent"
            android:layout_height="@dimen/dp_1"
            />

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/url_imgs_recycleview"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
		加载更多,高度30dp,隐藏和显示,框架都设置好了,只需要在布局中设置样式即可,
        <TextView
            android:layout_width="match_parent"
            android:layout_height="@dimen/dp_30"
            android:gravity="center"
            android:text="@string/loading"
            android:textSize="@dimen/sp_14"/>
    </com.scwang.smartrefresh.layout.SmartRefreshLayout>

</LinearLayout>

推荐这种加载view:
		         <com.scwang.smartrefresh.layout.SmartRefreshLayout
        android:id="@+id/fragment_refreshlayout"
        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"
            app:srlEnableLastTime="false"
            app:srlFinishDuration="300" />

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/rv_batch_select"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginHorizontal="@dimen/dp_12"
            android:overScrollMode="never" />

        <com.scwang.smartrefresh.layout.footer.ClassicsFooter
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:srlFinishDuration="200" />

    </com.scwang.smartrefresh.layout.SmartRefreshLayout>

java代码中,监听:
        refresh_layout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                refresh_layout.finishRefresh();
            }
        });
        refresh_layout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                DLog.d(TAG, "onLoadMore");
                getCollectList();
            }
        });
也可以关闭加载更多,或者刷新功能:
refresh_layout.setEnableRefresh(false);
        refresh_layout.setEnableLoadMore(false);






2、

<com.scwang.smartrefresh.layout.SmartRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/refreshLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#444444"
    app:srlPrimaryColor="#444444"
    app:srlAccentColor="@android:color/white"
    app:srlEnablePreviewInEditMode="true">
    <!--background s Footer 的背景色-->
    <!--srlAccentColor srlPrimaryColor 将会改变 Header 背景色-->
    <!--srlEnablePreviewInEditMode 可以开启和关闭预览功能-->
   
       <!--ClassicsHeader 还有其他3种样式,可自行选择-->
        <com.scwang.smartrefresh.layout.header.ClassicsHeader
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="20dp"
        android:background="@android:color/white"
        android:text="正在加载"/>
 <!--ClassicsFooter 还有其他2种样式,可自行选择-->
    <com.scwang.smartrefresh.layout.footer.ClassicsFooter
        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"-->
            <!--android:overScrollMode="never"-->
            <!--android:background="#fff" />-->
    </com.scwang.smartrefresh.layout.SmartRefreshLayout>

3、在main中:


 RefreshLayout refreshLayout = (RefreshLayout)findViewById(R.id.refreshLayout);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                refreshlayout.finishRefresh(2000/*,false*/);//传入false表示刷新失败
                Log.e(TAG, "onRefresh: " );//下拉刷新
            }
        });
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {
                refreshlayout.finishLoadMore(2000/*,false*/);//传入false表示加载失败
                Log.e(TAG, "onLoadMore: " );//上拉加载更多
            }
        });


详细的可参考:https://github.com/scwang90/SmartRefreshLayout

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jian11058

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值