前言
上拉加載下拉刷新的控件大家該很熟悉了坟奥,今天介紹一款不錯的上拉加載下拉刷新控件--------SmartRefreshLayout
參考鏈接:
SmartRefreshLayout官網(wǎng)
一.導(dǎo)入依賴
在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://設(shè)置Header主題顏色
app:srlPrimaryColor="#00000000"http://設(shè)置Footer主題顏色
app:srlEnablePreviewInEditMode="true"http://開啟和關(guān)閉預(yù)覽功能
三.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();
}
});
開啟/關(guān)閉下拉刷新和加載更多缠诅,結(jié)束刷新和加載
//開始下拉
mRefreshLayout.setEnableRefresh(true);//啟用刷新
mRefreshLayout.setEnableLoadmore(true);//啟用加載
//關(guān)閉下拉
mRefreshLayout.finishRefresh();
mRefreshLayout.finishLoadmore();
//自動下拉
mRefreshLayout.autoRefresh();
mRefreshLayout.autoLoadMore();
四.運(yùn)行效果
SmartRefreshLayout運(yùn)行的默認(rèn)效果如下
五.SmartRefreshLayout的其他的刷新樣式
#######5.1經(jīng)典樣式ClassicsFooter
只需要在布局中添加對應(yīng)的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>
運(yùn)行效果如下圖:
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>
運(yùn)行效果圖:
5.3header,footer各樣式類所引入包路徑如下
SmartRefreshLayout可以引入好幾種Header和Footer樣式并思,其中footer固定樣式有三種新博,在refresh-layout包下:
包路徑分別為:
com.scwang.smartrefresh.layout.footer.BallPulseFooter
com.scwang.smartrefresh.layout.footer.ClassicsFooter
com.scwang.smartrefresh.layout.footer.FalsifyFooter
header樣式則很多,在refresh-layout包下有:
包路徑分別為:
com.scwang.smartrefresh.layout.header.BezierRadarHeader
com.scwang.smartrefresh.layout.header.ClassicsHeader
com.scwang.smartrefresh.layout.header.FalsifyHeader
在refresh-header包下有以下樣式:
他們的包路徑是:
com.scwang.smartrefresh.header.BezierCircleHeader
com.scwang.smartrefresh.header.DeliveryHeader
//以下類似,在此省略
//......
六.自定義Header和Footer
當(dāng)然SmartRefreshLayout還支持自定義Header和Footer
具體可以參考官網(wǎng)中的自定義Header
SmartRefreshLayout關(guān)于屬性這一塊也是有很多可以設(shè)置的桃纯,大家依然可以去SmartRefreshLayout官網(wǎng)查看更多使用細(xì)則捣郊,這里就不展開講解了
今天就講到這里了,謝謝大家慈参。