效果:
1、依赖:
implementation 'com.scwang.smartrefresh:SmartRefreshLayout:1.1.0-alpha-23'
compile 'com.scwang.smartrefresh:SmartRefreshHeader:1.1.0-alpha-23'//没有使用特殊Header,可以不加这行
implementation 'com.android.support:recyclerview-v7:28.0.0'//recycleview依赖
2、xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.scwang.smartrefresh.layout.SmartRefreshLayout
android:id="@+id/url_imgs_refreshlayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
这里项目需求不需要刷新,就设置为1,设置为0会无效的
<TextView
android:layout_width="match_parent"
android:layout_height="@dimen/dp_1"
/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/url_imgs_recycleview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
加载更多,高度30dp,隐藏和显示,框架都设置好了,只需要在布局中设置样式即可,
<TextView
android:layout_width="match_parent"
android:layout_height="@dimen/dp_30"
android:gravity="center"
android:text="@string/loading"
android:textSize="@dimen/sp_14"/>
</com.scwang.smartrefresh.layout.SmartRefreshLayout>
</LinearLayout>
推荐这种加载view:
<com.scwang.smartrefresh.layout.SmartRefreshLayout
android:id="@+id/fragment_refreshlayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.scwang.smartrefresh.layout.header.ClassicsHeader
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srlEnableLastTime="false"
app:srlFinishDuration="300" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_batch_select"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginHorizontal="@dimen/dp_12"
android:overScrollMode="never" />
<com.scwang.smartrefresh.layout.footer.ClassicsFooter
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srlFinishDuration="200" />
</com.scwang.smartrefresh.layout.SmartRefreshLayout>
java代码中,监听:
refresh_layout.setOnRefreshListener(new OnRefreshListener() {
@Override
public void onRefresh(@NonNull RefreshLayout refreshLayout) {
refresh_layout.finishRefresh();
}
});
refresh_layout.setOnLoadMoreListener(new OnLoadMoreListener() {
@Override
public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
DLog.d(TAG, "onLoadMore");
getCollectList();
}
});
也可以关闭加载更多,或者刷新功能:
refresh_layout.setEnableRefresh(false);
refresh_layout.setEnableLoadMore(false);
2、
<com.scwang.smartrefresh.layout.SmartRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/refreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#444444"
app:srlPrimaryColor="#444444"
app:srlAccentColor="@android:color/white"
app:srlEnablePreviewInEditMode="true">
<!--background s Footer 的背景色-->
<!--srlAccentColor srlPrimaryColor 将会改变 Header 背景色-->
<!--srlEnablePreviewInEditMode 可以开启和关闭预览功能-->
<!--ClassicsHeader 还有其他3种样式,可自行选择-->
<com.scwang.smartrefresh.layout.header.ClassicsHeader
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp"
android:background="@android:color/white"
android:text="正在加载"/>
<!--ClassicsFooter 还有其他2种样式,可自行选择-->
<com.scwang.smartrefresh.layout.footer.ClassicsFooter
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<!--<android.support.v7.widget.RecyclerView-->
<!--android:id="@+id/recyclerView"-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="match_parent"-->
<!--android:overScrollMode="never"-->
<!--android:background="#fff" />-->
</com.scwang.smartrefresh.layout.SmartRefreshLayout>
3、在main中:
RefreshLayout refreshLayout = (RefreshLayout)findViewById(R.id.refreshLayout);
refreshLayout.setOnRefreshListener(new OnRefreshListener() {
@Override
public void onRefresh(RefreshLayout refreshlayout) {
refreshlayout.finishRefresh(2000/*,false*/);//传入false表示刷新失败
Log.e(TAG, "onRefresh: " );//下拉刷新
}
});
refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
@Override
public void onLoadMore(RefreshLayout refreshlayout) {
refreshlayout.finishLoadMore(2000/*,false*/);//传入false表示加载失败
Log.e(TAG, "onLoadMore: " );//上拉加载更多
}
});
详细的可参考:https://github.com/scwang90/SmartRefreshLayout