編輯:關於 Android 開發
談完 Fragment 的建立與生命週期,接著來看它最常被拿來做的事:畫面。Fragment 通常作為 Activity 介面的一部分,把自己的 layout 併入宿主的版面配置中。
要讓 Fragment 擁有畫面,必須實作 onCreateView() 回呼方法,並在其中回傳一個 View 物件作為該 Fragment 版面的根節點。
例外情況:如果你的 Fragment 是繼承自 ListFragment,就不必實作 onCreateView(),因為預設實作已經幫你回傳一個 ListView。
要把 XML 版面描述轉成 View 物件,onCreateView() 很貼心地提供了一個 LayoutInflater 參數:
public static class ExampleFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// 為這個 Fragment 攤開版面
return inflater.inflate(R.layout.example_fragment, container, false);
}
}
參數說明如下:
container:用來承放 Fragment 版面的父 ViewGroup,也就是它在 Activity 版面中的位置。savedInstanceState:一個 Bundle,用途與 Activity 的 onCreate() 中的 Bundle 相似,負責狀態回復。要留意的是,Fragment 的 onCreate() 也有一個 Bundle 參數,兩者承放的內容並不相同,切勿混用。inflate() 方法共三個參數:
false,因為系統稍後會自行把這個 View 放進 container;若傳 true,等於提前掛了一次,最終版面就會多出一層權限不到的 ViewGroup 而導致問題。Fragment 的版面會併入 Activity 的版面之中,實作上有兩條路可走。
使用 <fragment> 標籤即可,以下例子中一個 Activity 同時容納兩個 Fragment:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment android:name="com.example.news.ArticleListFragment"
android:id="@+id/list"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
<fragment android:name="com.example.news.ArticleReaderFragment"
android:id="@+id/viewer"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="match_parent" />
</LinearLayout>
系統在攤開這份版面時,會逐一實例化每個 <fragment>,呼叫它們的 onCreateView() 取得各自的 View,再將該 View 直接放回 <fragment> 所在的位置,取代原本的標籤節點。
注意:每個 Fragment 都需要一個識別碼,系統在 Activity 重建時靠它還原 Fragment,你也能用它來做移除等後續操作。提供識別碼有三種方式:
android:id 指定一個數字資源。android:tag 指定一個字串。這種做法的優點是可以執行期才決定要放什麼,只要事先在版面中預留一個空的 ViewGroup 當容器即可。所有變更都得透過 FragmentTransaction:
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
ExampleFragment fragment = new ExampleFragment();
fragmentTransaction.add(R.id.fragment_container, fragment);
fragmentTransaction.commit();
add() 的第一個參數是容器 ViewGroup 的 ID,第二個參數是要加入的 Fragment。只要透過 FragmentTransaction 動過 Fragment,就一定要呼叫 commit() 才算生效。
另外,tag 不只是無介面 Fragment 的專利。有畫面的 Fragment 同樣可以掛 tag,之後用 findFragmentByTag() 把它找回來。
前面示範的都是「有畫面」的 Fragment,但 Fragment 也能完全不顯示 UI,只在背景替 Activity 辦事——例如保留一個在旋轉螢幕後仍不被銷毀的工作執行緒,或封裝一段資料載入邏輯。
要加入無介面的 Fragment,請呼叫帶 tag 參數的多載版本:
add(Fragment fragment, String tag)
由於它沒有畫面,實作時自然不需要(也不該)回傳版面,onCreateView() 可以直接省略。而因為沒有 view ID 可用,tag 字串就成了找回這個 Fragment 的唯一線索,日後一律以 findFragmentByTag() 取得它。
Android 為空白包簽名:jarsigner 指令完整教學
Android 為空白包簽名:jarsigner 指令完整教學 前言:先前公司在騰訊開放平台認領應用時,遇到一個問題——需要為空白包簽名,再上傳審核。 騰訊開放平台的官方
Android 桌面快捷方式深度解析:適配碎片化 ROM 的實務指南
Android 桌面快捷方式深度解析:適配碎片化 ROM 的實務指南 桌面快捷方式是 App 導入流量的重要入口,也是許多產品希望用戶能快速回到應用的方式。然而,Andr
Android學習指南之十:Spinner、AutoCompleteTextView、DatePicker與TimePicker
本節繼續介紹用戶界面中的幾個常用控件:Spinner、AutoCompleteTextView、DatePicker和TimePicker。它們覆蓋了下拉選擇、輸入提示、
Android Camera 拍照實戰:從官方文檔到兼容性填坑全記錄
這篇記錄來自一次真實項目的拍照功能開發。選擇 Camera 而非 Camera2 的原因很實際:Camera2 從 API 21(Android 5.0)才引入,而 Ca