1.在 build.gradle 中添加依賴
implementation 'com.scwang.smartrefresh:SmartRefreshLayout:1.1.0-alpha-21'
implementation 'com.scwang.smartrefresh:SmartRefreshHeader:1.1.0-alpha-21'//沒(méi)有使用特殊Header扑庞,可以不加這行
//注意版本與項(xiàng)目一致
implementation 'com.android.support:recyclerview-v7:28.0.0'
2.在XML布局文件中更換為 SmartRefreshLayout
<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">
<ListView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:dividerHeight="20dp"
android:cacheColorHint="#00000000"
/>
</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表示加載失敗
}
});
4.使用指定的 Header 和 Footer
a.方法一 全局設(shè)置
public class App extends Application {
//static 代碼段可以防止內(nèi)存泄露
static {
//設(shè)置全局的Header構(gòu)建器
SmartRefreshLayout.setDefaultRefreshHeaderCreator(new DefaultRefreshHeaderCreator() {
@Override
public RefreshHeader createRefreshHeader(Context context, RefreshLayout layout) {
layout.setPrimaryColorsId(R.color.colorPrimary, android.R.color.white);//全局設(shè)置主題顏色
return new ClassicsHeader(context);//.setTimeFormat(new DynamicTimeFormat("更新于 %s"));//指定為經(jīng)典Header母债,默認(rèn)是 貝塞爾雷達(dá)Header
}
});
//設(shè)置全局的Footer構(gòu)建器
SmartRefreshLayout.setDefaultRefreshFooterCreator(new DefaultRefreshFooterCreator() {
@Override
public RefreshFooter createRefreshFooter(Context context, RefreshLayout layout) {
//指定為經(jīng)典Footer携栋,默認(rèn)是 BallPulseFooter
return new ClassicsFooter(context).setDrawableSize(20);
}
});
}
}
注意:方法一 設(shè)置的Header和Footer的優(yōu)先級(jí)是最低的,如果同時(shí)還使用了方法二祷蝌、三,將會(huì)被其它方法取代
b.方法二 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 將會(huì)改變 Header 和 Footer 的主題顏色-->
<!--srlEnablePreviewInEditMode 可以開(kāi)啟和關(guān)閉預(yù)覽功能-->
<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設(shè)置的Header和Footer的優(yōu)先級(jí)是中等的帆卓,會(huì)被方法三覆蓋巨朦。而且使用本方法的時(shí)候,Android Studio 會(huì)有預(yù)覽效果剑令,如下圖:
刷新預(yù)覽效果.jpg
不過(guò)不用擔(dān)心糊啡,只是預(yù)覽效果,運(yùn)行的時(shí)候只有下拉才會(huì)出現(xiàn)~
c.方法三 Java代碼設(shè)置
final RefreshLayout refreshLayout = (RefreshLayout) findViewById(R.id.refreshLayout);
//設(shè)置 Header 為 貝塞爾雷達(dá) 樣式
refreshLayout.setRefreshHeader(new BezierRadarHeader(this).setEnableHorizontalDrag(true));
//設(shè)置 Footer 為 球脈沖 樣式
refreshLayout.setRefreshFooter(new BallPulseFooter(this).setSpinnerStyle(SpinnerStyle.Scale));
5吁津、詳細(xì)參數(shù)進(jìn)此網(wǎng)站
https://github.com/scwang90/SmartRefreshLayout/blob/master/art/md_property.md
例子:
//取消下拉刷新
groupLayout.setEnableRefresh(false);