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

深入解析 Android ViewStub:延遲加載的適用邊界與實踐要點

編輯:關於 Android 程式設計

開發中經常遇到這種情形:某些 View 或某塊布局要不要顯示,得等運行時才能定。最直覺的寫法是把所有候選 View 都寫進布局,初始設為 View.GONE,再在代碼裡按需切換可見性。

這個做法邏輯直白、控制靈活,缺點是浪費資源。GONE 只影響顯示,Inflate 時該 View 依然會被解析、被實例化、被賦值——對象該建的都建了,記憶體該花的都花了。對於「大概率用不上」或者「只在特定分支才用」的布局,這筆開銷並不合理。

更合適的工具是 android.view.ViewStub。它是一個極輕量的、不可見且不佔布局位置的控件。你可以為它指定一個布局資源,Inflate 時只有 ViewStub 本身被創建;直到它被設為可見或顯式呼叫 inflate(),它所指向的布局才真正被解析與實例化,同時 ViewStub 自身的布局屬性會一併轉交給該布局。這樣就能把「加載時機」推遲到真正需要的那一刻。

不過 ViewStub 並非萬能,下面把它的特性與適用邊界講清楚。

ViewStub 的兩個關鍵特性

1. 只能 Inflate 一次。加載完成後 ViewStub 物件會被置空,也就是說,某個布局一旦被它加載出來,此後就再也無法通過這個 ViewStub 去控制它。

2. 只能加載一個布局文件,而非單個 View。當然,你可以把單個 View 單獨放進一個布局文件裡再交給它。

什麼時候該用 ViewStub

由上述特性可以推出兩個合適的場景:

場景一:某塊布局一旦加載就不再變化,除非重啟應用。因為 ViewStub 加載一次即失效,若你需要反覆顯示/隱藏同一塊內容,它做不到,這時只能回到「可見性控制」的老路。

場景二:要控制的是一個布局文件,而不是某個具體 View。ViewStub 的 android:layout 只接受布局 ID,無法直接指向某個 Button 或 TextView。

反過來說,如果目標是控制單個 View 的顯隱,或者需要運行時反覆切換,那就別用 ViewStub,老老实實用可見性。

一個實例

需求:頁面上要展示兩種互斥的內容之一——用 TextView 顯示一段文字,或用 ImageView 顯示一張圖片,並在 onCreate() 時二擇其一。這正是 ViewStub 的最佳落點。

主布局只放兩個 ViewStub:

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center_horizontal">

    <ViewStub
        android:id="@+id/viewstub_demo_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dip"
        android:layout_marginRight="5dip"
        android:layout_marginTop="10dip"
        android:layout="@layout/viewstub_demo_text_layout" />

    <ViewStub
        android:id="@+id/viewstub_demo_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dip"
        android:layout_marginRight="5dip"
        android:layout="@layout/viewstub_demo_image_layout" />
</LinearLayout>

文字布局:

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/viewstub_demo_textview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#aa664411"
        android:textSize="16sp" />
</LinearLayout>

圖片布局:

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/viewstub_demo_imageview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

代碼層的選擇非常輕:找到對應的 ViewStub,呼叫 inflate(),再回頭 findViewById 拿到真正的目標 View。

public class ViewStubDemoActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.viewstub_demo_activity);

        if ((((int) (Math.random() * 100)) & 0x01) == 0) {
            // 顯示文字:只需 inflate 文字那個 stub
            ViewStub stub = (ViewStub) findViewById(R.id.viewstub_demo_text);
            stub.inflate();
            TextView text = (TextView) findViewById(R.id.viewstub_demo_textview);
            text.setText("The tree of liberty must be refreshed from time to time"
                    + " with the blood of patriots and tyrants! Freedom is nothing but"
                    + " a chance to be better!");
        } else {
            // 顯示圖片:只需 inflate 圖片那個 stub
            ViewStub stub = (ViewStub) findViewById(R.id.viewstub_demo_image);
            stub.inflate();
            ImageView image = (ImageView) findViewById(R.id.viewstub_demo_imageview);
            image.setImageResource(R.drawable.happy_running_dog);
        }
    }
}

運行效果:

使用注意事項

有一類布局屬性必須寫在 ViewStub 上才會生效,寫在真實布局的根節點上反而無效——最典型的就是 android:layout_margin* 系列。比如上面的例子,如果把左右邊距加在 TextView 上不會起作用,必須放在 ViewStub 上。原因是這些屬性描述的是「本 View 在父容器中的位置」,而 inflate 之前真正佔位的是 ViewStub;inflate 之後,ViewStub 的屬性會整體移交給它加載出的布局。

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