1.导入依赖
implementation 'com.scwang.smartrefresh:SmartRefreshLayout:1.1.0-alpha-14'
//没有使用特殊Header和Footer,可以不加下面这行
implementation 'com.scwang.smartrefresh:SmartRefreshHeader:1.1.0-alpha-14'
//Recyclerview
implementation 'com.android.support:recyclerview-v7:28.0.0'
2.在activity_main.xml中添加布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<!--app:srlAccentColor="#00000000"//设置Header主题颜色
app:srlPrimaryColor="#00000000"//设置Footer主题颜色
app:srlEnablePreviewInEditMode="true"//开启和关闭预览功能-->
<com.scwang.smartrefresh.layout.SmartRefreshLayout
android:id="@+id/refreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.scwang.smartrefresh.header.DeliveryHeader
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<android.support.v7.widget.RecyclerView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:overScrollMode="never" />
<com.scwang.smartrefresh.layout.footer.ClassicsFooter
android:layout_width="match_parent"=
android:layout_height="wrap_content"
app:srlClassicsSpinnerStyle="Translate" />
</com.scwang.smartrefresh.layout.SmartRefreshLayout>
</LinearLayout>
3.MainActivity中使用
//加载更多
refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
@Override
public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
page ++ ;
initData();
adapter.notifyDataSetChanged();
refreshLayout.finishLoadMore(true);//加载完成
}
});
//刷新
refreshLayout.setOnRefreshListener(new OnRefreshListener() {
@Override
public void onRefresh(@NonNull RefreshLayout refreshLayout) {
list.clear();
initData();
adapter.notifyDataSetChanged();
refreshLayout.finishRefresh(true);//刷新完成
}
});
可以去SmartRefreshLayout官网查看更多使用细节
发现一个写的很好上拉加载下拉刷新所以https://www.jianshu.com/p/71811f0de259(转)