我们经常会碰见 正在加载中,加载出错, “暂无商品”等一系列的相似的布局,因为我们有很多请求网络数据的页面,我们不可能每一个页面都写几个“正在加载中”等布局吧,这时候将这些状态的布局封装在一起就很有必要了。我们可以将这些封装为一个自定布局,然后每次操作该自定义类的方法就行了。
首先一般来说,从服务器拉去数据之前都是“正在加载”页面, 加载成功之后“正在加载”页面消失,展示数据;如果加载失败,就展示“加载出错”页面,同样的“z正在加载”消失; 同理 数据为null的话,也是这样。如下图所示:
其实这只是一种代码设计模式,有这种思想就行,实现起来千变万化的。
总体思想就是:自定义FrameLayout ,然后将这FrameLayout中有三个子view,分别是:展示数据的view,加载失败的view, 正在加载的view,数据为null的view; 其实展示数据的view是一直VISIBLE的,切记:其他三个view都要设置为match_parent,然后父布局都要设置color ;
比如数据为null的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:background="@android:color/white">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:text="暂无数据!"/>
</LinearLayout>
父布局是match——parent 然后也设置了颜色;
在声明SynExceptionLayout 的XML中,要如下声明:
<?xml version="1.0" encoding="utf-8"?>
<com.app.test.testerrorproject.SynExceptionLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout