SmartRefreshLayout——上拉加載下拉刷新

效果圖展示

image

SmartRefreshLayout是一個用于上拉加載和下拉刷新的一個控件,通常結合RecyclerView一起使用

SmartRefreshLayout的目標是打造一個強大,穩(wěn)定,成熟的下拉刷新框架,并集成各種的炫酷、多樣、實用、美觀的Header和Footer。 正如名字所說,SmartRefreshLayout是一個“聰明”或者“智能”的下拉刷新布局,由于它的“智能”,它不只是支持所有的View,還支持多層嵌套的視圖結構。 它繼承自ViewGroup 而不是FrameLayout或LinearLayout,提高了性能。 也吸取了現(xiàn)在流行的各種刷新布局的優(yōu)點,包括谷歌官方的 SwipeRefreshLayout, 其他第三方的 Ultra-Pull-To-Refresh、TwinklingRefreshLayout 。 還集成了各種炫酷的 Header 和 Footer。支持所有的 View(AbsListView、RecyclerView、WebView....View)
支持自定義并且已經(jīng)集成了很多炫酷的 Header 和 Footer.

1.導入依賴

    implementation 'com.scwang.smartrefresh:SmartRefreshLayout:1.1.0-alpha-14'
    //沒有使用特殊Header和Footer,可以不加下面這行
    implementation 'com.scwang.smartrefresh:SmartRefreshHeader:1.1.0-alpha-14'

    //Recyclerview
    implementation 'com.android.support:recyclerview-v7:28.0.0'

2.在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"
    android:orientation="vertical"
    tools:context=".MainActivity">

 <!--app:srlAccentColor="#00000000"http://設置Header主題顏色
    app:srlPrimaryColor="#00000000"http://設置Footer主題顏色
    app:srlEnablePreviewInEditMode="true"http://開啟和關閉預覽功能-->

    <com.scwang.smartrefresh.layout.SmartRefreshLayout
        android:id="@+id/refreshLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <com.scwang.smartrefresh.header.DeliveryHeader
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <android.support.v7.widget.RecyclerView
            android:id="@+id/lv"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:overScrollMode="never" />

        <com.scwang.smartrefresh.layout.footer.ClassicsFooter
            android:layout_width="match_parent"=   
            android:layout_height="wrap_content"
            app:srlClassicsSpinnerStyle="Translate" />

    </com.scwang.smartrefresh.layout.SmartRefreshLayout>

</LinearLayout>

3.MainActivity中使用

        //加載更多
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page ++ ;
                initData();
                adapter.notifyDataSetChanged();
                refreshLayout.finishLoadMore(true);//加載完成
            }
        });
        //刷新
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                list.clear();
                initData();
                adapter.notifyDataSetChanged();
                refreshLayout.finishRefresh(true);//刷新完成
            }
        });

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)容

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