簡介
項目中經(jīng)常遇到這樣一種情況兵拢,新打開的界面需要加載數(shù)據(jù)翻斟,存在多種狀態(tài)的結(jié)果,需要根據(jù)不同結(jié)果展示界面说铃,這個過程歸納起來可以分為五種狀態(tài):初始狀態(tài)访惜、請求狀態(tài)、空數(shù)據(jù)狀態(tài)腻扇、網(wǎng)絡(luò)錯誤狀態(tài)债热、成功請求狀態(tài)。 如果多個界面都存在這個流程幼苛,那么封裝整個過程的調(diào)用就很有必要了窒篱,既可以簡化調(diào)用過程,又可以很方便的管理整個流程蚓峦。
StateFrameLayout
繼承自FrameLayout
舌剂,內(nèi)部實現(xiàn)了一句代碼切換各種狀態(tài)的功能(各狀態(tài)的布局均需要在xml指定,具有高度定制性)暑椰,并且內(nèi)部實現(xiàn)了狀態(tài)緩存霍转,無需擔心內(nèi)存回收后重新打開界面會導致狀態(tài)被重置(可打開手機開發(fā)者選項中的“不保留活動”來驗證)。
Github:https://github.com/Vanish136/StateFrameLayout
使用
Gradle中引用
compile 'com.lwkandroid:StateFrameLayout:1.0.1'
xml中定義
<com.lwkandroid.stateframelayout.StateFrameLayout
android:id="@+id/stateLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:enableContentAnim="true" //是否在展示內(nèi)容布局的時候開啟動畫(200ms的Alpha動畫)
app:emptyLayoutResId="@layout/layout_empty" //這里指定空數(shù)據(jù)布局
app:loadingLayoutResId="@layout/layout_loading" //這里指定加載過程的布局
app:netErrorLayoutResId="@layout/layout_net_error" //這里指定網(wǎng)絡(luò)錯誤的布局
>
<!--在這里定義內(nèi)容布局一汽,內(nèi)容布局只能有一個-->
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_green_dark">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="我是內(nèi)容"
android:textAppearance="?android:textAppearanceLarge"/>
</FrameLayout>
</com.lwkandroid.stateframelayout.StateFrameLayout>
備注: 此外避消,對于空數(shù)據(jù)和網(wǎng)絡(luò)錯誤的狀態(tài)提供了重試接口,需要在空數(shù)據(jù)或網(wǎng)絡(luò)錯誤布局中指定觸發(fā)的view對應(yīng)的id:android:id="@id/id_sfl_empty_retry"
或android:id="@id/id_sfl_net_error_retry"
<br />
代碼中使用
五種狀態(tài): <br />
-
StateFrameLayout.INIT
:初始狀態(tài)召夹,所有布局隱藏岩喷,默認切換的狀態(tài) <br /> -
StateFrameLayout.LOADING
:只顯示請求Loading布局 <br /> -
StateFrameLayout.EMPTY
:只顯示空數(shù)據(jù)布局 <br /> -
StateFrameLayout.NET_ERROR
:只顯示網(wǎng)絡(luò)錯誤布局 <br /> -
StateFrameLayout.SUCCESS
:只顯示請求成功后的內(nèi)容布局 <br />
StateFrameLayout mStateFrameLayout = (StateFrameLayout) findViewById(R.id.stateLayout);
//切換各種狀態(tài)
mStateFrameLayout.changeState(上面五種狀態(tài)中任何一個);
//是否在展示內(nèi)容布局的時候開啟動畫(200ms的Alpha動畫)
mStateFrameLayout.enableContentAnim(true);
//設(shè)置網(wǎng)絡(luò)錯誤重試監(jiān)聽
mStateFrameLayout.setOnNetErrorRetryListener(new StateFrameLayout.OnNetErrorRetryListener()
{
@Override
public void onNetErrorRetry()
{
//TODO 在這里相應(yīng)重試操作
}
});
//設(shè)置空數(shù)據(jù)重試監(jiān)聽
mStateFrameLayout.setOnEmptyRetryListener(new StateFrameLayout.OnEmptyRetryListener()
{
@Override
public void onEmptyRetry()
{
//TODO 在這里相應(yīng)重試操作
}
});
<br >
效果圖
![](https://github.com/Vanish136/StateFrameLayout/raw/master/pics/sample01.png)
![](https://github.com/Vanish136/StateFrameLayout/raw/master/pics/sample02.png)
![](https://github.com/Vanish136/StateFrameLayout/raw/master/pics/sample03.png)
![](https://github.com/Vanish136/StateFrameLayout/raw/master/pics/sample04.png)
<br />
參考
MaterialPageStateLayout: https://github.com/Syehunter/MaterialPageStateLayout <br />
感謝所有為開源做出貢獻的人們!