StateFrameLayout
StateFrameLayout:https://github.com/Vanish136/StateFrameLayout
demo:http://git.oschina.net/libraryDemo/StateFrameLayout01
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" //是否在展示内容布局的时候开启动画(200ms的Alpha动画)
app:emptyLayoutResId="@layout/layout_empty" //这里指定空数据布局
app:loadingLayoutResId="@layout/layout_loading" //这里指定加载过程的布局
app:netErrorLayoutResId="@layout/layout_net_error" //这里指定网络错误的布局
>
<!--在这里定义内容布局,内容布局只能有一个-->
<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="我是内容"
android:textAppearance="?android:textAppearanceLarge"/>
</FrameLayout>
</com.lwkandroid.stateframelayout.StateFrameLayout>
备注: 此外,对于空数据和网络错误的状态提供了重试接口,需要在空数据或网络错误布局中指定触发的view对应的id:android:id=”@id/id_sfl_empty_retry”或android:id=”@id/id_sfl_net_error_retry”
五种状态:
StateFrameLayout.INIT
:初始状态,所有布局隐藏,默认切换的状态StateFrameLayout.LOADING
:只显示请求Loading布局StateFrameLayout.EMPTY
:只显示空数据布局StateFrameLayout.NET_ERROR
:只显示网络错误布局StateFrameLayout.SUCCESS
:只显示请求成功后的内容布局
stateFrameLayout = (StateFrameLayout) findViewById(R.id.stateFrameLayout);
stateFrameLayout.setOnEmptyRetryListener(new StateFrameLayout.OnEmptyRetryListener() {
@Override
public void onEmptyRetry() {
Toast.makeText(context,"数据为空,重新请求数据",Toast.LENGTH_SHORT).show();
}
});
stateFrameLayout.setOnNetErrorRetryListener(new StateFrameLayout.OnNetErrorRetryListener() {
@Override
public void onNetErrorRetry() {
Toast.makeText(context,"网路错误,重新请求数据",Toast.LENGTH_SHORT).show();
}
});
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch (position){
case 0:
stateFrameLayout.changeState(StateFrameLayout.LOADING);
break;
case 1:
stateFrameLayout.changeState(StateFrameLayout.EMPTY);
break;
case 2:
stateFrameLayout.changeState(StateFrameLayout.NET_ERROR);
break;
case 3:
stateFrameLayout.changeState(StateFrameLayout.SUCCESS);
break;
}
}
});