前言
上拉加載下拉刷新的控件大家該很熟悉了,今天介紹一款不錯的上拉加載下拉刷新控件--------SmartRefreshLayout
參考鏈接:
SmartRefreshLayout官網(wǎng)
一.導入依賴
在app-module中添加RecycleView和SmartRefreshLayout的依賴
//recyclerview
implementation 'com.android.support:recyclerview-v7:26.1.0'
implementation 'com.android.support:design:26.1.0'
//SmartRefreshLayout
implementation 'com.scwang.smartrefresh:SmartRefreshLayout:1.0.4-7'
implementation 'com.scwang.smartrefresh:SmartRefreshHeader:1.0.4-7'
二.在mainActivity中添加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"
tools:context="com.freshdemo.MainActivity"
android:orientation="vertical">
<com.scwang.smartrefresh.layout.SmartRefreshLayout
android:id="@+id/refreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:srlAccentColor="#00000000"
app:srlPrimaryColor="#00000000"
app:srlEnablePreviewInEditMode="true">
<android.support.v7.widget.RecyclerView
android:id="@+id/rv"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</com.scwang.smartrefresh.layout.SmartRefreshLayout>
</LinearLayout>
這是SmartRefreshLayout的基本布局,其中:
app:srlAccentColor="#00000000"http://設置Header主題顏色
app:srlPrimaryColor="#00000000"http://設置Footer主題顏色
app:srlEnablePreviewInEditMode="true"http://開啟和關閉預覽功能
三.MainActivity中初始化和刷新加載事件
private RecyclerView mRecyclerView;
private RefreshLayout mRefreshLayout;
//初始化
mRecyclerView=findViewById(R.id.rv);
mRefreshLayout = findViewById(R.id.refreshLayout);
//刷新
mRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
@Override
public void onRefresh(RefreshLayout refreshlayout) {
mData.clear();
mNameAdapter.notifyDataSetChanged();
refreshlayout.finishRefresh();
}
});
//加載更多
mRefreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
@Override
public void onLoadmore(RefreshLayout refreshlayout) {
for(int i=0;i<30;i++){
mData.add("小明"+i);
}
mNameAdapter.notifyDataSetChanged();
refreshlayout.finishLoadmore();
}
});
開啟/關閉下拉刷新和加載更多,結束刷新和加載
//開始下拉
mRefreshLayout.setEnableRefresh(true);//啟用刷新
mRefreshLayout.setEnableLoadmore(true);//啟用加載
//關閉下拉
mRefreshLayout.finishRefresh();
mRefreshLayout.finishLoadmore();
//自動下拉
mRefreshLayout.autoRefresh();
mRefreshLayout.autoLoadMore();
四.運行效果
SmartRefreshLayout運行的默認效果如下

1.gif
五.SmartRefreshLayout的其他的刷新樣式
#######5.1經(jīng)典樣式ClassicsFooter
只需要在布局中添加對應的header和footer,則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"
tools:context="com.freshdemo.MainActivity"
android:orientation="vertical">
<com.scwang.smartrefresh.layout.SmartRefreshLayout
android:id="@+id/refreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:srlAccentColor="#00000000"
app:srlPrimaryColor="#00000000"
app:srlEnablePreviewInEditMode="true">
<com.scwang.smartrefresh.layout.header.ClassicsHeader
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/rv"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<com.scwang.smartrefresh.layout.footer.ClassicsFooter
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</com.scwang.smartrefresh.layout.SmartRefreshLayout>
</LinearLayout>
運行效果如下圖:

2.gif
5.2一個絢麗的PhoenixHeader展示
修改后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"
tools:context="com.freshdemo.MainActivity"
android:orientation="vertical">
<com.scwang.smartrefresh.layout.SmartRefreshLayout
android:id="@+id/refreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:srlAccentColor="#00000000"
app:srlPrimaryColor="#00000000"
app:srlEnablePreviewInEditMode="true">
<com.scwang.smartrefresh.header.PhoenixHeader
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/rv"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<com.scwang.smartrefresh.layout.footer.BallPulseFooter
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</com.scwang.smartrefresh.layout.SmartRefreshLayout>
</LinearLayout>
運行效果圖:

3.gif
5.3header,footer各樣式類所引入包路徑如下
SmartRefreshLayout可以引入好幾種Header和Footer樣式,其中footer固定樣式有三種,在refresh-layout包下:

image.png
包路徑分別為:
com.scwang.smartrefresh.layout.footer.BallPulseFooter
com.scwang.smartrefresh.layout.footer.ClassicsFooter
com.scwang.smartrefresh.layout.footer.FalsifyFooter
header樣式則很多,在refresh-layout包下有:

image.png
包路徑分別為:
com.scwang.smartrefresh.layout.header.BezierRadarHeader
com.scwang.smartrefresh.layout.header.ClassicsHeader
com.scwang.smartrefresh.layout.header.FalsifyHeader
在refresh-header包下有以下樣式:

image.png
他們的包路徑是:
com.scwang.smartrefresh.header.BezierCircleHeader
com.scwang.smartrefresh.header.DeliveryHeader
//以下類似,在此省略
//......
六.自定義Header和Footer
當然SmartRefreshLayout還支持自定義Header和Footer
具體可以參考官網(wǎng)中的自定義Header
SmartRefreshLayout關于屬性這一塊也是有很多可以設置的,大家依然可以去SmartRefreshLayout官網(wǎng)查看更多使用細則,這里就不展開講解了
今天就講到這里了,謝謝大家。