我们常常遇到这样的一种需求:当后台返回的数据正常时,展示数据,数据为空时,展示一种空状态布局,当网络异常时,展示“网络不见了”的布局,如果你的Activity只有一个页面,实现起来当然是极为简单的,伪代码如下:
<Root>
<!--空页面布局-->
<include
android:id="@+id/layout_empty"
layout="@layout/layout_empty"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout>
//主布局
</LinearLayout>
</Root>
然后再Activity/Fragment中写:
fun changePageState(){
when(pageStatus){
1->{
// 正常页面
}
2->{
// 显示空布局
}
}
}
但现实是,我们一个App中包含了数十个甚至上百个页面,如果每个页面都这么写,当然不是不可以哈,就是有点繁琐,那我们能否只在基类中操作ÿ