Android 中通用的Toolbar和Error,Empty,Loading UI 處理

在Android 開發(fā)中Activity幾乎都有Toolbar(menu也在里面)和以及Http 請求的時(shí)候出現(xiàn)的Error,Empty,Loading等UI 需要處理,怎么能快速簡單高效處理呢?


image.png

首先定義BaseActivity 中的XML 定義

如下面所示,默認(rèn)都是需要Toolbar 的,如果不需要就設(shè)置為不可見就好了;
而不同的Activity 中的內(nèi)容實(shí)際是放在(FrameLayout)fl_content 中的。

R.layout.activity_base

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical">
    <!--默認(rèn)都是需要Toolbar 的,如果不需要就設(shè)置為不可見就好了-->
    <android.support.v7.widget.Toolbar
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/toolbar"
        android:layout_height="?attr/actionBarSize"
        android:layout_width="match_parent"
        android:background="?attr/colorPrimary"
        app:titleTextColor="@android:color/white"
        app:subtitleTextColor="@color/light_white"
        app:navigationIcon="@drawable/ic_back_copy">
    </android.support.v7.widget.Toolbar>

    <!-- 不同的頁面展示插到這里來-->
    <FrameLayout
        android:id="@+id/fl_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>

怎么處理不同的Activity 中的內(nèi)容實(shí)際是放在(FrameLayout)fl_content 中的呢?

   @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = BaseActivity.this;

        View rootView=customContentView(View.inflate(this, R.layout.activity_base, null));
        setContentView(rootView);

        initViews();
        initHttp();  //在這里進(jìn)行Http 的請求
    }


    /**
     * 定制Custom View,Content 區(qū)域先留空,后面再動態(tài)的添加,同時(shí)
     * 增加Error,empty,Loading,timeout,等通用的場景處理,一處Root注入,處處可用
     *
     */
    private View customContentView(View rootView) {
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        if (mToolbar != null) {
            setSupportActionBar(mToolbar);
        }

        View content = View.inflate(this, getLayoutId(), null);
        if (content != null) {
            FrameLayout flContent = (FrameLayout) rootView.findViewById(R.id.fl_content);
            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
                    FrameLayout.LayoutParams.MATCH_PARENT);
            flContent.addView(content, params);
            ButterKnife.bind(this, rootView);   //ButterKnife 綁定

            //增加Error,empty,Loading,timeout,等通用的場景處理
            mBaseLoadService = LoadSir.getDefault().register(content, new Callback.OnReloadListener() {
                @Override
                public void onReload(View v) {
                    onHttpReload(v);
                }
            });
        }
        return rootView;
    }

在加載數(shù)據(jù)的時(shí)候會出現(xiàn)Error,empty,Loading,timeout 等場景怎么處理呢?

這種問題幾乎每個(gè)頁面都會遇到,難道每個(gè)頁面都是使用FrameLayout 疊加兩層內(nèi)容來處理???,推薦一個(gè)項(xiàng)目LoadSir:https://github.com/KingJA/LoadSir

在BaseActivity 中已在 LoadSir.getDefault().register(content 了

    private View customContentView(View rootView) {
        View content = View.inflate(this, getLayoutId(), null);
        if (content != null) {
            FrameLayout flContent = (FrameLayout) rootView.findViewById(R.id.fl_content);
            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
                    FrameLayout.LayoutParams.MATCH_PARENT);
            flContent.addView(content, params);

            //增加Error,empty,Loading,timeout,等通用的場景處理
            mBaseLoadService = LoadSir.getDefault().register(content, new Callback.OnReloadListener() {
                @Override
                public void onReload(View v) {
                    onHttpReload(v);
                }
            });
        }
        return rootView;
    }

使用的時(shí)候只要大概這樣就好了:

        /**
         * mBaseLoadService,很方便的就能展示空啊什么的;
         *
         */
        if (data == null || data.size() == 0) {
            mBaseLoadService.showCallback(EmptyCallback.class);
        } else {
            mBaseLoadService.showSuccess();
        }

Demo on github:AndroidAppFrameWork(代碼估計(jì)能加快理解)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容