需求:
刚打开界面时,由于要请求网络数据。界面总不能空白着吧,就需要一个正在加载中的提示语,安抚用户着急的心理
效果图:
思路:
这就没法提供完整代码,可以提供思路。正常大点的项目都会有基类,在基类的xml里面做操作。刚打开的时候显示正在加载布局,加载完数据隐藏正在加载布局,显示正常布局(一般隐藏即可,自然就显示正常布局了,相当于正在加载布局是盖在上面的)
关键代码:
fragment_base.xml
<?xml version="1.0" encoding="utf-8"?><!--BaseFragment用的布局,主要是针对界面需要网络数据来显示而用的-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ll_main_base"
android:layout_width="match_parent"
android:fitsSystemWindows="true"
android:layout_height="match_parent"
android:orientation="vertical">
<!--头部-->
<include layout="@layout/layout_head_base" />
<!--正在加载...-->
<include layout="@layout/layout_loading_base" />
<!--网络或者请求出错-->
<include layout="@layout/layout_net_error_base" />
</LinearLayout>
正在加载提示语就是layout_loading_base
layout_loading_base.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ll_progress_base"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:gravity="center">
<ProgressBar
android:layout_width="@dimen/d40px"
android:layout_height="@dimen/d40px" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/loading"
android:textColor="@color/c_66" />
</LinearLayout>
看下在fragment中的使用
public void showRealView() {
LogUtils.i("showRealViewB",this.getClass().getSimpleName());
try {
isLoadedOnce = true;
if (!isShowRealViewContinueLoading && null != llProgress) {
llProgress.setVisibility(View.GONE);
}
if (null != errorView) {
errorView.setVisibility(View.GONE);
}
if (realView == null && layoutId > 0) {
//子类要现实的布局
realView = BaseUtils.inflate(mContext, layoutId,vgMain);
vgMain.addView(realView);
}
if(bind == null){
//绑定控件
bind = ButterKnife.bind(this, vgMain);
initRealView(vgMain);
initPrClick();
}
}catch (Exception e){
e.printStackTrace();
}
}
上面代码中llProgress就是layout_loading_base,errorView是layout_net_error_base,vgMain是ll_main_base。比如请求接口完成后,就可以隐藏正常加载布局和错误布局,添加真正布局进去。
虽然这次案例没法直接拿去用,仅做参考了。