Android使用下拉刷新框架SmartRefreshLayout

该文章展示了如何在Android项目中集成SmartRefreshLayout库,用于实现下拉刷新和上拉加载的效果。通过在build.gradle文件中添加依赖,设置布局XML文件,以及在MainActivity中配置和监听刷新事件,实现了对RecyclerView的加载操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

效果

添加依赖 build.gradle(app)


    // 下拉刷新,上拉加载
    implementation 'com.scwang.smartrefresh:SmartRefreshLayout:1.1.3'
    // 没有使用特殊Header,可以不加这一依赖
    implementation 'com.scwang.smartrefresh:SmartRefreshHeader:1.1.3'

在settings.gradle添加 maven { url 'https://www.jitpack.io' }

dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
        maven { url 'https://www.jitpack.io' }
    }
}

实现代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/activity_main"
    android:fitsSystemWindows="true"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:background="@color/purple_500"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        app:navigationIcon="?attr/homeAsUpIndicator"
       >
    </androidx.appcompat.widget.Toolbar>

    <com.scwang.smartrefresh.layout.SmartRefreshLayout
        android:id="@+id/refreshLayout"
        android:background="#22000000"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:scaleY="-1"
        app:srlEnableRefresh="false"
        app:srlEnableAutoLoadMore="false"
        app:srlEnableNestedScrolling="false">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/rv"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:overScrollMode="never"
            android:scrollbars="none"
            android:fadeScrollbars="false"
            android:dividerHeight="0dp"
            android:divider="@android:color/transparent"
            tools:itemCount="1"
            android:scaleY="-1"
            app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />

        <com.scwang.smartrefresh.layout.footer.ClassicsFooter
            android:id="@+id/footer"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scaleY="-1"
            app:srlTextPulling="下拉加载更多">
        </com.scwang.smartrefresh.layout.footer.ClassicsFooter>

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



    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="5dip"
        android:background="#f8f8f8"
        android:gravity="center_vertical">

        <TextView
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:text="还没有刷新"
            android:textSize="30dp"
            android:layout_gravity="center"
            android:gravity="center"/>
    </LinearLayout>


</LinearLayout>

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.impl.ScrollBoundaryDeciderAdapter;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;


public class MainActivity extends AppCompatActivity {

    private Toolbar toolbar ;
    private RefreshLayout refreshLayout;
    private TextView text;
    private Handler handler;
    private int num = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //去掉顶部标题
        getSupportActionBar().hide();
        setContentView(R.layout.activity_main);

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        text = (TextView) findViewById(R.id.text);

        handler = new MyHandler();

        refreshLayout = findViewById(R.id.refreshLayout);
        refreshLayout.setEnableRefresh(false);//必须关闭
        refreshLayout.setEnableAutoLoadMore(true);//必须关闭
        refreshLayout.setEnableNestedScroll(false);//必须关闭
        refreshLayout.setEnableScrollContentWhenLoaded(true);//必须关闭
        refreshLayout.getLayout().setScaleY(-1);//必须设置
        refreshLayout.setScrollBoundaryDecider(new ScrollBoundaryDeciderAdapter() {
            @Override
            public boolean canLoadMore(View content) {
                return super.canRefresh(content);//必须替换
            }
        });

        //监听加载,而不是监听 刷新
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull final RefreshLayout refreshLayout) {
                refreshLayout.getLayout().postDelayed(new Runnable() {
                    @Override
                    public void run() {


                        // 执行操作

                        // 发到主线程中 收到的数据
                        Message message = Message.obtain();
                        message.what = 1;
                        message.obj = ++num;
                        handler.sendMessage(message);

                        refreshLayout.finishLoadMore();

                    }
                }, 2000);
            }
        });

        toolbar.setTitle("刷新");

        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        /*
         * 触发测试
         */
        toolbar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                refreshLayout.autoLoadMore();
            }
        });


    }


    private class MyHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            if (msg.what == 1) {
                System.out.println("收到消息:"+msg.obj.toString());
                int num =  (int)msg.obj;

                text.setText("刷新了"+num+"次");
            }

        }
    }


}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Android 《开发》

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

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

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

打赏作者

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

抵扣说明:

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

余额充值