Android提高布局加載性能ViewStub的快速實戰(zhàn)應用

在APP的開發(fā)過程中我們往往會遇到?jīng)]有數(shù)據(jù)或者沒有網(wǎng)絡情況時使用一個布局view提示當前數(shù)據(jù)加載的情況 ,就如下圖所示##

沒有數(shù)據(jù)時得顯示占位圖
有數(shù)據(jù)時正常顯示圖

我們可以用平常使用的布局來寫也可以實現(xiàn),但是這對于我們布局的加載會存在一定的壓力,所以我們使用ViewStub來加載。

那么什么是ViewStub呢?

ViewStub是一個輕量級的View,沒有尺寸,它不繪制任何東西,因此繪制或者移除時更省時。(ViewStub不可見,大小為0)
當然,這是我用簡短的話語描述的,你也可也參照官方文檔了解更多使用方法點擊進入ViewStub使用官網(wǎng)

我不想浪費更多時間去順著文檔翻譯一遍(有興趣的自己去研讀,官網(wǎng)鏈接需要翻墻進入),目的只有一個快速寫入項目中

首先看看布局文件吧!

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <ImageView
        android:id="@+id/empty_icon"
        android:layout_centerInParent="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/img_kong"/>

    <TextView
        android:id="@+id/empty_text"
        android:layout_marginTop="10dp"
        android:layout_centerInParent="true"
        android:layout_below="@+id/empty_icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/hint_line"
        android:text="當前暫無數(shù)據(jù)"/>

</RelativeLayout>

在使用的布局文件中通過ViewStub引入上面的布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fitsSystemWindows="true"
    android:background="@color/white"
    android:orientation="vertical" >

    <include
        android:id="@+id/include"
        layout="@layout/title_bar" />

    <TextView
        android:id="@+id/tv_kid_online"
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:gravity="center_vertical"
        android:layout_marginLeft="10dp"
        android:text=""
        android:textColor="@color/content_hint_text"
        android:textSize="14sp" />

    <ListView
        android:divider="@color/white"
        android:cacheColorHint="@color/white"
        android:dividerHeight="0dp"
        android:listSelector="@android:color/transparent"
        android:id="@+id/lv_kid_online"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerHorizontal="true" >

    </ListView>
    
    <ViewStub
        android:id="@+id/emptyView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:inflatedId="@+id/view_id_after_inflate"
        android:layout="@layout/view_empty" />

</LinearLayout>

下面是在activity中區(qū)初始化,以及通常的使用方法

private ViewStub stubView;
private View emptyView;
 stubView = (ViewStub) findViewById(R.id.emptyView);

在activity中使用如下方法

    //顯示數(shù)據(jù)空的效果
    private void showErrorView() {
        if (emptyView == null) {
            //ViewStub 只能加載一次
            emptyView = stubView.inflate();
            TextView text = (TextView) findViewById(R.id.empty_text);
            text.setText("當前園所沒有設置攝像頭,請聯(lián)系園所設置");
        }
        emptyView.setVisibility(View.VISIBLE);
        listView.setVisibility(View.GONE);
    }

  //隱藏數(shù)據(jù)空的效果
    private void hintErrorView() {
        if (emptyView == null) {
            emptyView = stubView.inflate();
        }
        emptyView.setVisibility(View.GONE);
        listView.setVisibility(View.VISIBLE);
    }

最后你只需要在需要在比如數(shù)據(jù)為空,或者沒有網(wǎng)絡的時候使用showErrorView();這個方法就能夠顯示圖片上的效果,反之使用hintErrorView();隱藏該視圖,text.setText("當前園所沒有設置攝像頭,請聯(lián)系園所設置");這個地方添加自己想使用的提示語,或者增加圖片更換也是同樣的道理

最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容