前言
本文主要講解一下SmartRefreshLayout的一些基礎使用和常用設置剔难,包括如何自定義header胆屿、footer等。
更加具體的使用方式可以到SmartRefreshLayout官網(wǎng)查看偶宫。
一非迹、簡單使用方法
1.在 build.gradle 中添加依賴
/**
* 刷新布局分成三個包啦,用到哪個引用哪個就行
* SmartRefreshLayout 刷新布局核心實現(xiàn)纯趋,自帶ClassicsHeader(經(jīng)典)憎兽、BezierRadarHeader(貝塞爾雷達)兩個 Header.
* SmartRefreshHeader 各種Header的集成,除了Layout自帶的Header吵冒,其它都在這個包中.
* SmartRefreshFooter 各種Footer的集成纯命,除了Layout自帶的Footer,其它都在這個包中.
*/
//1.1.0 API改動過大痹栖,老用戶升級需謹慎
compile 'com.scwang.smartrefresh:SmartRefreshLayout:1.1.0-alpha-21'//基本的功能有這行就夠了
compile 'com.scwang.smartrefresh:SmartRefreshHeader:1.1.0-alpha-21'//沒有使用特殊Header亿汞,可以不加這行
compile 'com.android.support:appcompat-v7:25.3.1'//版本 23以上(必須)
//1.1.0 androidx 版本
implementation 'com.scwang.smartrefresh:SmartRefreshLayout:1.1.0-andx-4'
implementation 'com.scwang.smartrefresh:SmartRefreshHeader:1.1.0-andx-4'
//1.0.5 當1.1.0出現(xiàn)問題可以回退到1.0.5.1
compile 'com.scwang.smartrefresh:SmartRefreshLayout:1.0.5.1'
compile 'com.scwang.smartrefresh:SmartRefreshHeader:1.0.5.1'//沒有使用特殊Header,可以不加這行
compile 'com.android.support:appcompat-v7:25.3.1'//版本 23以上(必須)
compile 'com.android.support:design:25.3.1'//版本隨意(非必須揪阿,引用可以解決無法預覽問題)
2.在XML布局文件中添加 SmartRefreshLayout
<?xml version="1.0" encoding="utf-8"?>
<com.scwang.smartrefresh.layout.SmartRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/refreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<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.在 Activity 或者 Fragment 中添加代碼
RefreshLayout refreshLayout = (RefreshLayout)findViewById(R.id.refreshLayout);
refreshLayout.setOnRefreshListener(new OnRefreshListener() {
@Override
public void onRefresh(RefreshLayout refreshlayout) {
refreshlayout.finishRefresh(2000/*,false*/);//傳入false表示刷新失敗
}
});
refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
@Override
public void onLoadMore(RefreshLayout refreshlayout) {
refreshlayout.finishLoadMore(2000/*,false*/);//傳入false表示加載失敗
}
});
二留夜、如何使用指定的 Header 和 Footer匙铡,有三種方法
1.方法一 全局設置
public class App extends Application {
//static 代碼段可以防止內存泄露
static {
//設置全局的Header構建器
SmartRefreshLayout.setDefaultRefreshHeaderCreator(new DefaultRefreshHeaderCreator() {
@Override
public RefreshHeader createRefreshHeader(Context context, RefreshLayout layout) {
layout.setPrimaryColorsId(R.color.colorPrimary, android.R.color.white);//全局設置主題顏色
return new ClassicsHeader(context);//.setTimeFormat(new DynamicTimeFormat("更新于 %s"));//指定為經(jīng)典Header图甜,默認是 貝塞爾雷達Header
}
});
//設置全局的Footer構建器
SmartRefreshLayout.setDefaultRefreshFooterCreator(new DefaultRefreshFooterCreator() {
@Override
public RefreshFooter createRefreshFooter(Context context, RefreshLayout layout) {
//指定為經(jīng)典Footer碍粥,默認是 BallPulseFooter
return new ClassicsFooter(context).setDrawableSize(20);
}
});
}
}
注意:方法一 設置的Header和Footer的優(yōu)先級是最低的,如果同時還使用了方法二黑毅、三嚼摩,將會被其它方法取代
2.方法二 XML布局文件指定
<com.scwang.smartrefresh.layout.SmartRefreshLayout
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">
<!--srlAccentColor srlPrimaryColor 將會改變 Header 和 Footer 的主題顏色-->
<!--srlEnablePreviewInEditMode 可以開啟和關閉預覽功能-->
<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="@dimen/dimenPaddingCommon"
android:background="@android:color/white"
android:text="@string/description_define_in_xml"/>
<com.scwang.smartrefresh.layout.footer.ClassicsFooter
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</com.scwang.smartrefresh.layout.SmartRefreshLayout>
注意:方法二 XML設置的Header和Footer的優(yōu)先級是中等的,會被方法三覆蓋矿瘦。而且使用本方法的時候枕面,Android Studio 會有預覽效果,如下圖:
不過不用擔心缚去,只是預覽效果潮秘,運行的時候只有下拉才會出現(xiàn)~
3.方法三 Java代碼設置
final RefreshLayout refreshLayout = (RefreshLayout) findViewById(R.id.refreshLayout);
//設置 Header 為 貝塞爾雷達 樣式
refreshLayout.setRefreshHeader(new BezierRadarHeader(this).setEnableHorizontalDrag(true));
//設置 Footer 為 球脈沖 樣式
refreshLayout.setRefreshFooter(new BallPulseFooter(this).setSpinnerStyle(SpinnerStyle.Scale));