編輯:關於 Android 程式設計
本篇承接上一篇談 include 標籤的內容(:
https://blog.csdn.net/jason0539/article/details/26131831),接著說明當初在版面中出現的 ViewStub 這個控制項。
官方對 ViewStub 的定位是:一個不可見、大小為零的 View,最適合拿來實作 View 的延遲加載,避免一開始就把用不到的資源全部載入,等到真正需要時才 inflate。
關鍵特性是:一旦 ViewStub 被加載,它本身就會被新加載進來的 View 取代,原來的 id 隨之消失。
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="30dp"
layout="@layout/header" />
<ViewStub android:id="@+id/pic_stub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:inflatedId="@+id/pic_view_id_after_inflate"
android:layout="@layout/pic_view" />
<include layout="@layout/footer" />
</RelativeLayout>
兩個屬性最要緊:
android:inflatedId 是加載完成後新 View 的 id。若日後想用 findViewById 取得這個 View,必須使用 inflatedId,因為原本的 id 已經不存在了。
android:layout 則指定要加載進來的版面配置。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView android:id="@+id/iv_pic"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/pic" />
</LinearLayout>
內容很單純,就一張圖片。這正是 ViewStub 的典型場景:一張佔用大量記憶體的大圖,不需要在啟動時就解碼。
public class MainActivity extends Activity {
private ViewStub pic_sub;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pic_sub = (ViewStub) findViewById(R.id.pic_stub);
new Timer().schedule(new TimerTask() {
@Override
public void run() {
handler.sendEmptyMessage(1);
}
}, 1000); // 延遲 1 秒後才加載
handler = new Handler() {
public void handleMessage(android.os.Message msg) {
// ① inflate 後直接取得新 View
View pic_view = pic_sub.inflate();
// ② 若改用 setVisibility 觸發,順序不可顛倒
// pic_sub.setVisibility(View.VISIBLE);
ImageView iv_pic = (ImageView) pic_view.findViewById(R.id.iv_pic);
iv_pic.setImageResource(R.drawable.pic);
// ③ 這裡會是 null
View view = findViewById(R.id.pic_stub);
// ④ 用 inflatedId 才拿得到
view = findViewById(R.id.pic_view_id_after_inflate);
}
};
}
}
① inflate():呼叫時,被載入的 View 會取代 ViewStub 本身,並由 inflate() 直接回傳這個新 View。因此應用不需要再額外執行一次 findViewById() 去取得參考。
② setVisibility 的陷阱:若改用 setVisibility(View.VISIBLE) 來觸發加載,那麼 inflate 與 setVisibility 兩行的順序不可顛倒,否則會出現 IllegalStateException: ViewStub must have a non-null ViewGroup viewParent。原因是 ViewStub 只能被 inflate 一次,而 setVisibility 內部其實是間接呼叫了 inflate。
③ findViewById(R.id.pic_stub):這裡會得到 null,因為 ViewStub 已被替換掉,pic_stub 這個 id 在版面樹中已不復存在。
④ findViewById(R.id.pic_view_id_after_inflate):使用 inflatedId 則能正常取到加載後的 View。
判斷標準很簡單:這塊 UI 是否有可能永不出現,或至少不需要在啟動時就準備好?例如錯誤提示區塊、詳細資訊面板、少見的功能入口,都很適合交給 ViewStub。反之,若某個元件幾乎一定會顯示,用 ViewStub 反而多了一層 inflate 的延遲與程式碼複雜度,不如直接寫在版面裡。
作者:jason0539
部落格:
https://blog.csdn.net/jason0539
Android ViewPager 進階使用技巧
從基礎翻頁到自定義指示器 ViewPager 除了基本的左右滑動,還可以搭配標題、指示器與監聽器,做出更接近正式產品的頁面切換效果。常見做法包括 PagerTabStri
Android Gallery 仿圖像集瀏覽效果
Android 內建的 Gallery 能提供流暢的圖片瀏覽體驗,但若想做出更貼近系統相簿的滑動效果,可以自行實作一個仿 Gallery 的圖像集瀏覽元件。基本原理這種效
Android 4.4 鼠標光標自訂與動態切換實作指南
自訂鼠標光標的目標在 Android 4.4 中,若要讓特定應用顯示專屬鼠標圖示,或依情境切換光標樣式,需要從系統資源、JNI、Framework Java 層到應用層廣
Android 桌面 Widget 開發要點解析(時間日期 Widget)
最近需要撰寫一個日期時間的桌面 Widget 來關聯日曆程式。由於過去較少接觸桌面 Widget,對這塊技術並不十分熟悉,於是趁機重新整理,並把製作一個簡易時間日期 Wi