上拉加載下拉刷新---SmartRefreshLayout

前言

上拉加載下拉刷新的控件大家該很熟悉了,今天介紹一款不錯的上拉加載下拉刷新控件--------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)查看更多使用細則,這里就不展開講解了

今天就講到這里了,謝謝大家。

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

相關閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,663評論 19 139
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,234評論 25 708
  • 發(fā)現(xiàn) 關注 消息 iOS 第三方庫、插件、知名博客總結 作者大灰狼的小綿羊哥哥關注 2017.06.26 09:4...
    肇東周閱讀 15,446評論 4 61
  • 母親過了年88歲,虔敬各路神靈。前天逢集,兄弟于花市買水仙二十株。母親見了甚是歡喜。今日花開,母親分出一株,供于佛...
    陳大劉七王十四閱讀 491評論 0 1

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