可能是最優雅的切換布局的方法
StateLayout用法
演示

演示圖
依賴
第一步,在項目根目錄的build.gradle加入,如果沒有,請加入
allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
} 
  第二步,依賴此庫
compile 'com.github.fingdo:stateLayout:1.0.0' 
  使用方法
引入布局
用法與SrcollView一致,只允許一個 根布局
<com.fngdo.statelayout.StateLayout
        android:id="@+id/state_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <!-- 內容布局 one root view -->
</com.fngdo.statelayout.StateLayout> 
  布局設置圖標和文字
<declare-styleable name="StateLayout">
    <!-- 錯誤提示圖標 -->
    <attr name="errorImg" format="reference" />
    <!-- 錯誤提示文字 -->
    <attr name="errorText" format="string" />
    <!-- 空數據提示圖標 -->
    <attr name="emptyImg" format="reference" />
    <!-- 空數據提示文字 -->
    <attr name="emptyText" format="string" />
    <!-- 沒有網絡提示圖標 -->
    <attr name="noNetworkImg" format="reference" />
    <!-- 沒有網絡提示文字 -->
    <attr name="noNetworkText" format="string" />
    <!-- 超時提示圖標 -->
    <attr name="timeOutImg" format="reference" />
    <!-- 超時提示文字 -->
    <attr name="timeOutText" format="string" />
    <!-- 登錄提示圖標 -->
    <attr name="loginImg" format="reference" />
    <!-- 登錄提示文字 -->
    <attr name="loginText" format="string" />
    <!-- 加載提示文字 -->
    <attr name="loadingText" format="string" />
</declare-styleable> 
  示例:
<com.fngdo.statelayout.StateLayout
    xmlns:sl="http://schemas.android.com/apk/res-auto"
    android:id="@+id/state_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    sl:emptyImg="@drawable/ic_state_empty"
    sl:emptyText="空數據提示文字"
    sl:errorImg="@drawable/ic_state_error"
    sl:errorText="錯誤提示文字"
    sl:loadingText="加載提示文字"
    sl:loginImg="@drawable/ic_state_login"
    sl:loginText="登錄提示文字"
    sl:noNetworkImg="@drawable/ic_state_no_network"
    sl:noNetworkText="沒有網絡提示文字"
    sl:timeOutImg="@drawable/ic_state_time_out"
    sl:timeOutText="超時提示文字">
</com.fngdo.statelayout.StateLayout> 
  代碼提前設置圖標和文字
//type為StateLayout的固定Type變量
public static final int ERROR = 1;
public static final int EMPTY = 2;
public static final int TIMEOUT = 3;
public static final int NOT_NETWORK = 4;
public static final int LOADING = 5;
public static final int LOGIN = 6; 
  
image
代碼設置顯示布局
//展示沒有網絡的界面
stateLayout.showNoNetworkView();
//展示超時的界面
stateLayout.showTimeoutView();
//展示空數據的界面
stateLayout.showEmptyView();
//展示錯誤的界面
stateLayout.showErrorView();
//展示登錄的界面
stateLayout.showLoginView();
//如下圖所示
1,直接顯示
2,設置提示stringId和圖片Id顯示
3,設置提示stringId顯示
4,設置提示字符串現實
5,設置提示字符串和圖片Id顯示</code></pre> 
  

 
  image
 
  
//顯示加載界面
stateLayout.showLoadingView();
1,直接顯示
2,設置提示stringId顯示
3,設置提示字符串現實
4,設置自定義加載View現實,如:
    1)進度條
    2)顯示gif的View
    3)自定義布局View</code></pre> 
  

 
  image
 
  
//顯示自定義界面
stateLayout.showCustomView();
 
  設置替換成自定義的界面:
 
  
 
  image
 
  設置切換界面動畫
 
  動畫默認為 false ,如果需要開啟動畫,請調用
 
  
//開啟動畫
stateLayout.setUseAnimation(true);
 
  如果用戶不設置自定義動畫,一般為默認的 漸隱縮放 動畫
 
  如果用戶需要設置動畫,請調用
 
  
//設置動畫
stateLayout.setViewSwitchAnimProvider(new FadeScaleViewAnimProvider());
 
  stateLayout 自定義了兩種動畫
 
  
//漸隱縮放,漸顯放大動畫
FadeScaleViewAnimProvider
//漸隱漸顯動畫
FadeViewAnimProvider
 
  用戶如需自定義動畫樣式,請實現 ViewAnimProvider 接口
 
  重寫 showAnimation 和 hideAnimation 方法。
 
  
//以FadeViewAnimProvider為例
public class FadeViewAnimProvider implements ViewAnimProvider {
@Override
public Animation showAnimation() {
    Animation animation = new AlphaAnimation(0.0f,1.0f);
    animation.setDuration(200);
    animation.setInterpolator(new DecelerateInterpolator());
    return animation;
}
@Override
public Animation hideAnimation() {
    Animation animation = new AlphaAnimation(1.0f,0.0f);
    animation.setDuration(200);
    animation.setInterpolator(new AccelerateDecelerateInterpolator());
    return animation;
}
}</code></pre> 
  
監聽刷新和登錄點擊
 
  請實現 StateLayout 里面的 OnViewRefreshListener 接口。
 
  重寫兩個方法:
 
  
//刷新界面
void refreshClick();
//登錄點擊
void loginClick();</code></pre> 
  
 
 
   
 
   
 
   
                    
 本文由用戶 dumujiang 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
                         轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
                         本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!