SmartRefreshLayout上拉加載下拉刷新(基礎(chǔ)篇)

前言

本文主要講解一下SmartRefreshLayout的一些基礎(chǔ)使用和常用設(shè)置,包括如何自定義header、footer等。
更加具體的使用方式可以到SmartRefreshLayout官網(wǎng)查看。

一、簡單使用方法

1.在 build.gradle 中添加依賴

 /**
* 刷新布局分成三個包啦,用到哪個引用哪個就行
* SmartRefreshLayout 刷新布局核心實現(xiàn),自帶ClassicsHeader(經(jīng)典)、BezierRadarHeader(貝塞爾雷達(dá))兩個 Header.
* SmartRefreshHeader 各種Header的集成,除了Layout自帶的Header,其它都在這個包中.
* SmartRefreshFooter 各種Footer的集成,除了Layout自帶的Footer,其它都在這個包中.
*/

//1.1.0 API改動過大,老用戶升級需謹(jǐn)慎
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 當(dāng)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'//版本隨意(非必須,引用可以解決無法預(yù)覽問題)

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.方法一 全局設(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)先級是最低的,如果同時還使用了方法二、三,將會被其它方法取代

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 可以開啟和關(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)先級是中等的,會被方法三覆蓋。而且使用本方法的時候,Android Studio 會有預(yù)覽效果,如下圖:

jpg_preview_xml_define.jpg

不過不用擔(dān)心,只是預(yù)覽效果,運行的時候只有下拉才會出現(xiàn)~

3.方法三 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));

三、常用的樣式

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容