XStateController,幾近完美的狀態控制機來襲

erose_lu 7年前發布 | 8K 次閱讀 安卓開發 Android開發 移動開發

StateController 顧名思義:狀態控制機,用作幾個狀態的切換。該控件可以在 Loading & Empty & Error & Content 四個狀態的自定義動畫切換。 

 

特性

  • 不受布局層級的控制,可任意大小、任意層級
  • 支持xml設置狀態布局資源
  • 支持java代碼設置狀態布局
  • 可自定義動畫切換狀態
  • 可注冊狀態切換回調
  • 支持異常情況的狀態保存
  • 可以設置一個或幾個狀態,無需全部狀態設置

集成步驟

step1

在根項目的 build.gradle 文件中添加

allprojects {
        repositories {
            ...
            maven { url 'https://jitpack.io' }
    }
}

step2

添加依賴

dependencies {
       compile 'compile 'com.github.limedroid:XStateController:v1.1.0'
}

screenshot.png

使用

<cn.droidlover.xstatecontroller.XStateController
        android:id="@+id/controller"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:x_emptyLayoutId="@layout/view_empty">


        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#854678"
            android:gravity="center"
            android:text="Content"
            android:textColor="@android:color/white"
            android:textSize="28sp" />

    </cn.droidlover.xstatecontroller.XStateController>

說明

  • 當 XStateController 僅有一個子view(ViewGroup)時,該子view則為 ContentView
  • 可以通過xml屬性設置布局,也可代碼設置

attrs設置狀態布局資源

<attr name="x_contentLayoutId" format="reference" />
 <attr name="x_loadingLayoutId" format="reference" />
 <attr name="x_emptyLayoutId" format="reference" />
 <attr name="x_errorLayoutId" format="reference" />
  • x_contentLayoutId 設置內容布局資源
  • x_loadingLayoutId 設置加載中狀態的布局資源
  • x_emptyLayoutId 設置空狀態的布局資源
  • x_errorLayoutId 設置錯誤狀態的布局資源

常見api

設置各狀態的布局資源

loadingView(loadingView);  //設置加載中的view
emptyView(emptyView);     //設置空view
errorView(errorView);    //設置errorView
contentView(contentView); //設置contentView

狀態切換

showLoading();        //切換到loading狀態
showError();        //切換到error狀態
showEmpty();        //切換到empty狀態
showContent();        //切換到content狀態

注冊狀態切換回調

registerStateChangeListener(xx);

回調的定義

public interface OnStateChangeListener {
        //狀態切換時回調,oldState當前狀態,newState目標狀態
        void onStateChange(int oldState, int newState);
        //動畫,可自定義切換動畫
        void animationState(View exitView, View enterView);
    }

使用時可直接繼承 SimpleStateChangeListener

 

 

 

 本文由用戶 erose_lu 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!