1、首先在build.gradle里面添加以下依赖库:
implementation 'com.scwang.smartrefresh:SmartRefreshLayout:1.1.0-alpha-21'
implementation 'com.scwang.smartrefresh:SmartRefreshHeader:1.1.0-alpha-21'//没有使用特殊Header,可以不加这行
//注意版本与项目一致
implementation 'com.android.support:recyclerview-v7:29.0.0'
2、然后在Activity的onCreate方法里添加以下代码:
//上下拉刷新
RefreshLayout refreshLayout = (RefreshLayout)view.findViewById(R.id.refreshLayout);
refreshLayout.setOnRefreshListener(new OnRefreshListener() {
@Override
public void onRefresh(RefreshLayout refreshlayout) {
refreshlayout.finishRefresh(2000/*,false*/);//传入false表示刷新失败
System.out.println("===下拉刷新结束");
}
});
refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
@Override
public void onLoadMore(RefreshLayout refreshlayout) {
refreshlayout.finishLoadMore(2000/*,false*/);//传入false表示加载失败
System.out.println("===上拉加载结束");
}
});
3、上下拉刷新-隐藏取消命令:
//取消下拉刷新
refreshLayout.setEnableRefresh(false);
//取消上拉更多
refreshLayout.setEnableLoadMore(false);
4、不知道有没有用的方法:
// //设置 Header 为 贝塞尔雷达 样式
// refreshLayout.setRefreshHeader(new BezierRadarHeader(getActivity()).setEnableHorizontalDrag(true));
// //设置 Footer 为 球脉冲 样式
// refreshLayout.setRefreshFooter(new BallPulseFooter(getActivity()).setSpinnerStyle(SpinnerStyle.Scale));
5、activity.xml实现方法com.scwang.smartrefresh.layout.SmartRefreshLayout :
<?xml version="1.0" encoding="utf-8"?>
<com.scwang.smartrefresh.layout.SmartRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/refreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/list_view"
>
</ListView>
</com.scwang.smartrefresh.layout.SmartRefreshLayout>
网友评论