android:用refresh-layout-kernel实现下拉刷新(原SmartRefreshLayout)
一,安装第三方库
官网:
https://gitee.com/scwang90/SmartRefreshLayout#english--%E4%B8%AD%E6%96%87
库地址:
https://mvnrepository.com/artifact/io.github.scwang90/refresh-layout-kernel
编辑build.gradle
在dependencies段添加以下三项,如下
// https://mvnrepository.com/artifact/io.github.scwang90/refresh-layout-kernel
implementation 'io.github.scwang90:refresh-layout-kernel:2.1.1'
// https://mvnrepository.com/artifact/io.github.scwang90/refresh-header-classics
implementation 'io.github.scwang90:refresh-header-classics:2.1.1'
// https://mvnrepository.com/artifact/io.github.scwang90/refresh-header-material
implementation 'io.github.scwang90:refresh-header-material:2.1.1'
然后点击Sync Now
二,代码:
xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activity.FreshActivity">
<com.scwang.smart.refresh.layout.SmartRefreshLayout
android:id="@+id/refreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:srlEnableLoadMore="false">
<com.scwang.smart.refresh.header.ClassicsHeader
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srlTextPulling="下拉可以刷新"
app:srlTextRelease="释放立刻刷新"
app:srlTextLoading="正在加载"
app:srlTextRefreshing="正在刷新"
app:srlTextUpdate="上次更新 M-d HH:mm"
app:srlTextFinish="刷新成功"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00ff00"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="300dp"
android:background="#fff0f0"
android:orientation="horizontal">
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="300dp"
android:background="#ff0000"
android:orientation="horizontal">
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="400dp"
android:background="#0000ff"
android:orientation="horizontal">
</LinearLayout>
</LinearLayout>
</com.scwang.smart.refresh.layout.SmartRefreshLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
java
package com.example.okdemo1.activity;
import android.os.Bundle;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import com.example.okdemo1.R;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
public class FreshActivity extends AppCompatActivity {
private SmartRefreshLayout refreshLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_fresh);
refreshLayout = (SmartRefreshLayout) findViewById(R.id.refreshLayout);
// 配置下拉刷新监听器
refreshLayout.setOnRefreshListener(refreshlayout -> {
refreshlayout.finishRefresh(2000);// 模拟网络延迟2秒后完成刷新
});
// 配置上拉加载监听器(如果需要)
refreshLayout.setOnLoadMoreListener(refreshlayout -> {
refreshlayout.finishLoadMore(2000);// 模拟网络延迟2秒后完成加载更多
});
}
}
三,测试效果:

浙公网安备 33010602011771号