Android開發完整項目案例-剛打開界面正在加載中..的提示

需求:

剛打開界面時,由於要請求網絡數據。界面總不能空白着吧,就需要一個正在加載中的提示語,安撫用戶着急的心理

效果圖:

思路:

這就沒法提供完整代碼,可以提供思路。正常大點的項目都會有基類,在基類的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。比如請求接口完成後,就可以隱藏正常加載佈局和錯誤佈局,添加真正佈局進去。

雖然這次案例沒法直接拿去用,僅做參考了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章