天天看點

下拉重新整理控件一個很好用的開源項目SmartRefreshLayout

簡單用例(注意引用遠端倉庫會有點問題,不行就引用源檔案)

1.在 buld.gradle 中添加依賴

compile 'com.android.support:design:25.3.1'//版本随意(非必須,引用可以解決無法預覽問題)
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,可以不加這行

//新版本預覽版-可能不穩定
compile 'com.scwang.smartrefresh:SmartRefreshLayout:1.0.4-alpha-5'
compile 'com.scwang.smartrefresh:SmartRefreshHeader:1.0.4-alpha-5'
           

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);
    }
});
refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
    @Override
    public void onLoadmore(RefreshLayout refreshlayout) {
        refreshlayout.finishLoadmore(2000);
    }
});      

使用指定的 Header 和 Footer

1.方法一 全局設定

public class App extends Application {
    //static 代碼段可以防止記憶體洩露
    static {
        //設定全局的Header建構器
        SmartRefreshLayout.setDefaultRefreshHeaderCreater(new DefaultRefreshHeaderCreater() {
                @Override
                public RefreshHeader createRefreshHeader(Context context, RefreshLayout layout) {
                    layout.setPrimaryColorsId(R.color.colorPrimary, android.R.color.white);//全局設定主題顔色
                    return new ClassicsHeader(context).setSpinnerStyle(SpinnerStyle.Translate);//指定為經典Header,預設是 貝塞爾雷達Header
                }
            });
        //設定全局的Footer建構器
        SmartRefreshLayout.setDefaultRefreshFooterCreater(new DefaultRefreshFooterCreater() {
                @Override
                public RefreshFooter createRefreshFooter(Context context, RefreshLayout layout) {
                    //指定為經典Footer,預設是 BallPulseFooter
                    return new ClassicsFooter(context).setSpinnerStyle(SpinnerStyle.Translate);
                }
            });
    }
}      

注意:方法一 設定的Header和Footer的優先級是最低的,如果同時還使用了方法二、三,将會被其它方法取代

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 可以開啟和關閉預覽功能-->
    <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/padding_common"
        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設定的Header和Footer的優先級是中等的,會被方法三覆寫。而且使用本方法的時候,Android Studio 會有預覽效果,如下圖:

下拉重新整理控件一個很好用的開源項目SmartRefreshLayout

不過不用擔心,隻是預覽效果,運作的時候隻有下拉才會出現~

3.方法三 Java代碼設定

final RefreshLayout refreshLayout = (RefreshLayout) findViewById(R.id.refreshLayout);
//設定 Header 為 Material樣式
refreshLayout.setRefreshHeader(new MaterialHeader(this).setShowBezierWave(true));
//設定 Footer 為 球脈沖
refreshLayout.setRefreshFooter(new BallPulseFooter(this).setSpinnerStyle(SpinnerStyle.Scale));      

混淆

SmartRefreshLayout 沒有使用到:序列化、反序列化、JNI、反射,是以并不需要添加混淆過濾代碼,并且已經混淆測試通過,如果你在項目的使用中混淆之後出現問題,請及時通知我。