1. 程式人生 > >最強大的下拉重新整理框架_SmartRefreshLayout的基本使用

最強大的下拉重新整理框架_SmartRefreshLayout的基本使用

介紹

支援下拉重新整理、上拉載入、RefreshLayout、OverScroll,Android智慧下拉重新整理框架,支援越界回彈,具有極強的擴充套件性,集成了幾十種炫酷的Header和 Footer。

不只是如其它的重新整理佈局所說的支援所有的View,還支援多層巢狀的檢視結構。 除了“聰明”之外,SmartRefreshLayout還具備了很多的特點。
它繼承自ViewGroup 而不是其它的FrameLayout或者LinearLayout,提高了效能。

https://github.com/scwang90/SmartRefreshLayout
支援所有的 View(AbsListView、RecyclerView、WebView...
.View) 和多層巢狀的檢視結構 支援自定義並且已經集成了很多炫酷的 Header 和 Footer (圖). 支援和ListView的同步滾動 和 RecyclerView、AppBarLayout、CoordinatorLayout 的巢狀滾動 NestedScrolling. 支援在Android Studio Xml 編輯器中預覽 效果(圖) 支援分別在 Default(預設)、Xml、JavaCode 三個中設定 Header 和 Footer. 支援自動重新整理、自動上拉載入(自動檢測列表慣性滾動到底部,而不用手動上拉). 支援通用的重新整理監聽器 OnRefreshListener 和更詳細的滾動監聽 OnMultiPurposeListener.
支援自定義回彈動畫的插值器,實現各種炫酷的動畫效果. 支援設定主題來適配任何場景的App,不會出現炫酷但很尷尬的情況. 支援設定多種滑動方式來適配各種效果的Header和Footer:平移、拉伸、背後固定、頂層固定、全屏 支援內容尺寸自適應 Content-wrap_content 支援繼承重寫和擴充套件功能,內部實現沒有 private 方法和欄位,繼承之後都可以重寫覆蓋 支援越界回彈(Listview、RecyclerView、ScrollView、WebView...View) 支援多點觸控,下拉、上拉各種手勢衝突
功能方法:
https://github.com/scwang90/SmartRefreshLayout/blob/master/art/md_property.md

導包

    compile 'com.android.support:appcompat-v7:25.3.1'//版本隨意
    compile 'com.scwang.smartrefresh:SmartRefreshLayout:1.0.3'
    compile 'com.scwang.smartrefresh:SmartRefreshHeader:1.0.3'//沒有使用特殊Header,可以不加這行

下拉重新整理

//可以包含任意佈局和巢狀佈局
<com.scwang.smartrefresh.layout.SmartRefreshLayout
    android:id="@+id/refresh"
    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.junx.smartrefreshlayout.MainActivity">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

</com.scwang.smartrefresh.layout.SmartRefreshLayout>
處理下拉重新整理事件
private SmartRefreshLayout refresh;
        refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                new Timer().schedule(new TimerTask() {
                    @Override
                    public void run() {
                        //結束載入
                        refresh.finishRefresh();
                        //載入失敗的話3秒後結束載入
                refreshlayout.finishRefresh(3000);
                    }
                },3000);
            }
        });

上拉重新整理

//用法同上
<com.scwang.smartrefresh.layout.SmartRefreshLayout
    android:id="@+id/refresh"
    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.junx.smartrefreshlayout.MainActivity">

    <ListView
        android:id="@+id/lv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

</com.scwang.smartrefresh.layout.SmartRefreshLayout>
       //為lv填充資料,只有資料填充到底部時候才會觸發上拉載入更多
        lv = (ListView) findViewById(R.id.lv);
        ArrayList<String> datas = new ArrayList<>();
        for (int i = 0; i < 50; i++) {
            datas.add("哈哈"+i);
        }
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,android.R.id.text1,datas);
        lv.setAdapter(adapter);     
        //進行載入更多的邏輯處理
        refresh.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                new Timer().schedule(new TimerTask() {
                    @Override
                    public void run() {
                        refresh.finishLoadmore();
                    }
                }, 3000);
            }
        });

新增頭佈局和腳佈局

優先順序 三>二>一
方法一:
        //設定頭佈局樣式,全域性有效
        SmartRefreshLayout.setDefaultRefreshHeaderCreater(new DefaultRefreshHeaderCreater() {
            @Override
            public RefreshHeader createRefreshHeader(Context context, RefreshLayout layout) {
                //全域性設定主題顏色
                layout.setPrimaryColorsId(R.color.colorPrimary, android.R.color.white);
                //指定為經典Header,預設是 貝塞爾雷達Header
                return new ClassicsHeader(context).setSpinnerStyle(SpinnerStyle.Translate);
            }
        });
        //設定腳佈局樣式,全域性有效
        SmartRefreshLayout.setDefaultRefreshFooterCreater(new DefaultRefreshFooterCreater() {
            @Override
            public RefreshFooter createRefreshFooter(Context context, RefreshLayout layout) {
                //指定為經典Footer,預設是 BallPulseFooter
                return new ClassicsFooter(context).setSpinnerStyle(SpinnerStyle.Translate);
            }
        });
方法二:
<?xml version="1.0" encoding="utf-8"?>
<com.scwang.smartrefresh.layout.SmartRefreshLayout
    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:id="@+id/refresh"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.junx.smartrefreshlayout.MainActivity">
    <!--在這裡設定頭佈局-->
    <com.scwang.smartrefresh.header.PhoenixHeader
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <!--也可以用控制元件設定頭佈局,比例使用gifview就可以實現萬能的頭佈局-->    
    <TextView
        android:text="我是頭佈局"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>    
    <ListView
        android:id="@+id/lv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>
    <!--在這裡設定腳佈局-->
    <com.scwang.smartrefresh.layout.footer.ClassicsFooter
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</com.scwang.smartrefresh.layout.SmartRefreshLayout>
方法三:
RefreshLayout refreshLayout = (RefreshLayout) findViewById(R.id.smartLayout);
refreshLayout.setRefreshHeader(new MaterialHeader(this).setShowBezierWave(true));
refreshLayout.setRefreshFooter(new BallPulseFooter(this).setSpinnerStyle(SpinnerStyle.Scale));

屬性設定

請訪問
https://github.com/scwang90/SmartRefreshLayout/blob/master/art/md_property.md