Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

ViewStub 的惰性載入:讓重量級布局只在真正需要時才進記憶體

編輯:關於 Android 程式設計

先前介紹過 <include> 標籤。它可以在布局檔中引用另一個布局檔,並覆寫被引用布局根節點上所有與布局相關的屬性(也就是以 android:layout 開頭的那些)。靠著它,一個龐大的布局檔可以拆成若干較小的片段,而這些片段還能被重複引用,達到重複利用的目的。

<include> 很方便,但有個結構性問題:布局中的控制項不一定在程式啟動時就會用到,有些只在特定情境下才出現。舉例來說,看書軟體只有在下載電子書時才需要進度條,平常閱讀的都是本機檔案,根本用不著。既然如此,啟動時完全可以先不載入這個進度條。然而 <include> 不管三七二十一,一律把所有控制項都載入記憶體。

或許有人覺得一個進度條吃不了多少資源,多載也無所謂。這種說法在單一案例中可能成立,但如果被載入的不是進度條,而是好幾個顯示大圖的 ImageView,而且還散落在多個地方被重複載入,那手機的資源恐怕很快就被榨乾了。因此我們需要一種機制來改變 <include> 的行為,做到「只在需要時才載入」——這正是 ViewStub 存在的意義。

ViewStub 與 include 的唯一差別

ViewStub 是一個不可視的控制項,作用與 <include> 基本相同,同樣在布局檔中以 <ViewStub> 標籤引用其他布局檔。唯一的差別是:它不會立刻載入被引用的布局,只有在呼叫 ViewStub.inflate() 或 ViewStub.setVisibility(View.VISIBLE) 之後才會真正載入。

先看兩個布局檔

main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="我的按鈕"
        android:onClick="onClick_Button" />

    <include layout="@layout/custom" />
</LinearLayout>

custom.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="按鈕1" />

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="按鈕2" />
</LinearLayout>

用 <include> 引用 custom.xml 時,畫面上會立刻出現三個按鈕。

若把 <include> 換成下面這段寫法,程式啟動時只會顯示 main.xml 裡定義的那一個按鈕:

<ViewStub
    android:id="@+id/viewstub"
    android:inflatedId="@+id/button_layout"
    android:layout="@layout/custom"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />

觸發載入

使用 <ViewStub> 引用布局後,還需要呼叫 inflate() 或 setVisibility(View.VISIBLE),被引用的控制項才會真正進到畫面:

public void onClick_Button(View v) {

    // ViewStub 只能取得一次;第二次用 findViewById 會回傳 null
    View view = findViewById(R.id.viewstub);

    if (view != null) {
        // 也可以改呼叫 inflate:
        // view = ((ViewStub) view).inflate();

        // 載入 custom.xml 中的控制項
        ((ViewStub) view).setVisibility(View.VISIBLE);
    } else {
        setTitle("view is null");
    }
}

點擊「我的按鈕」之後,custom.xml 中定義的兩個按鈕便會出現,效果與直接使用 <include> 完全相同。

三個容易搞混的細節

一、id 的用途不同。<ViewStub> 與 <include> 都能覆寫被引用布局根節點的布局屬性,但 <include> 的 android:id 是「直接取代」根節點原本的 id;而 <ViewStub> 的 android:id 與一般控制項一樣,是用來在程式碼中「參照這個 ViewStub 本身」。若你想置換被載入布局根節點的 id,必須改用 android:inflatedId。

二、ViewStub 只能用一次。載入之後,它會把自己從父容器中移除,並以 inflate 出來的檢視取而代之。所以第二次呼叫 findViewById(R.id.viewstub) 會得到 null,程式碼裡必須做防呆處理,否則容易觸發空參照。

三、ViewStub 目前仍無法取代 merge。它幾乎可以完全取代 <include>,唯一的不足是還做不到 <merge> 那種「消除多餘階層」的效果,兩者在布局優化上仍有分工。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved