效果

添加依赖 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+"次");
}
}
}
}