android之LoadingLayout

前言

項目裏都會遇到幾種頁面,分別爲加載中、無網絡、無數據、出錯四種情況,經常要使用,所以封成庫引用了,方便使用,順便分享出來。先看一下效果:


原理比較簡單,繼承FrameLayout,在xml渲染完成後,加上加載中、無網絡、無數據、出錯四個頁面,根據需要控制顯示哪一層。

使用方式

gradle引用:

compile 'com.lai.weavey:loadinglayout:1.3.1'

使用說明

LoadingLayout支持全局配置,對所有使用到的地方都起效,需要在Application中配置,如下:

 public class App extends Application {

    @Override
    public void onCreate() {
        super.onCreate();

        LoadingLayout.getConfig()
                .setErrorText("出錯啦~請稍後重試!")
                .setEmptyText("抱歉,暫無數據")
                .setNoNetworkText("無網絡連接,請檢查您的網絡···")
                .setErrorImage(R.mipmap.define_error)
                .setEmptyImage(R.mipmap.define_empty)
                .setNoNetworkImage(R.mipmap.define_nonetwork)
                .setAllTipTextColor(R.color.gray)
                .setAllTipTextSize(14)
                .setReloadButtonText("點我重試哦")
                .setReloadButtonTextSize(14)
                .setReloadButtonTextColor(R.color.gray)
                .setReloadButtonWidthAndHeight(150,40);
    }
}

由於“加載中”的頁面,可能每個App都不一樣,因此,LoadingLayout支持自定義LoadingPage,如下:

 LoadingLayout.getConfig()
     .setLoadingPageLayout(R.layout.define_loading_page);

同時,爲了適應個別界面的“特殊需求”,LoadingLayout也支持局部設置各種屬性,僅對當前對象生效,不影響全局。如下:

        LoadingLayout  loading = (LoadingLayout) findViewById(R.id.loading_layout);
        loading.setLoadingPage(R.layout.define_loading_page)
                .setEmptyText("暫無報告數據")
                .setErrorText("")
                .setNoNetworkText("")
                .setErrorImage(R.mipmap.ic_launcher)
                .setErrorTextSize(16)
                .setReloadButtonText("點我重新加載哦"); //等等

爲ReloadButton設置監聽:

loadingLayout.setOnReloadListener(new LoadingLayout.OnReloadListener() {
            @Override
            public void onReload(View v) {

            }
        });

設置顯示的頁面:

 loadingLayout.setStatus(LoadingLayout.Loading);//加載中
 loadingLayout.setStatus(LoadingLayout.Empty);//無數據
 loadingLayout.setStatus(LoadingLayout.Error);//錯誤
 loadingLayout.setStatus(LoadingLayout.No_Network);//無網絡
 loadingLayout.setStatus(LoadingLayout.Success);//加載成功

最後,在xml裏面使用:

<com.weavey.loading.lib.LoadingLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:isFirstVisible="true">

    <TextView
        android:background="@color/colorPrimary"
        android:visibility="visible"
        android:gravity="center"
        android:textColor="@android:color/white"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="ContentView"/>

</com.weavey.loading.lib.LoadingLayout>

注意:
(1)isFirstVisible屬性用來控制contentView一開始是否隱藏,由於LoadingLayout原理是在xml渲染完成後在contentView上鋪上三層View,因此,一開始如果不隱藏,等contentView渲染完成後調用: loadingLayout.setStatus(LoadingLayout.Loading);
會造成界面閃爍的效果,影響體驗,因此默認將contentView隱藏,所以數據加載完成後一定要調用loadingLayout.setStatus(LoadingLayout.Success);,將contentView顯示出來。這樣也能解決未獲取到數據的情況下,被用戶看到雜亂無章的佈局,個人還是比較喜歡默認隱藏contentView;
(2)爲了方便管理,LoadingLayout只能有一個直屬子View,類似ScrollView,添加兩個直屬子View會拋出異常throw new IllegalStateException("LoadingLayout can host only one direct child");
(3)由於AS會直接將自定義View的特性反應在預覽界面,所以在使用LoadingLayout的時候,會無法看到被LoadingLayout包裹住的佈局(默認爲gone),因此也可以將isFirstVisible屬性暫時設爲true,預覽佈局。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章