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

Fragment 開發實戰(三):Fragment 介面的實作與兩種掛載方式

編輯:關於 Android 開發

談完 Fragment 的建立與生命週期,接著來看它最常被拿來做的事:畫面。Fragment 通常作為 Activity 介面的一部分,把自己的 layout 併入宿主的版面配置中。

一、為 Fragment 準備版面

要讓 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() 方法共三個參數:

  1. 版面資源的 ID。
  2. 承放該版面的父 ViewGroup。
  3. 布林值,表示攤開版面時是否同時把它附加到 container 上。此處傳入 false,因為系統稍後會自行把這個 View 放進 container;若傳 true,等於提前掛了一次,最終版面就會多出一層權限不到的 ViewGroup 而導致問題。

二、把 Fragment 放進 Activity

Fragment 的版面會併入 Activity 的版面之中,實作上有兩條路可走。

方法一:在版面 XML 中直接宣告

使用 <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,你也能用它來做移除等後續操作。提供識別碼有三種方式:

  1. 為 android:id 指定一個數字資源。
  2. 為 android:tag 指定一個字串。
  3. 兩者都沒給,系統會改用該 Fragment 所在容器的 ID。

方法二:在程式碼中動態加入 ViewGroup

這種做法的優點是可以執行期才決定要放什麼,只要事先在版面中預留一個空的 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,但 Fragment 也能完全不顯示 UI,只在背景替 Activity 辦事——例如保留一個在旋轉螢幕後仍不被銷毀的工作執行緒,或封裝一段資料載入邏輯。

要加入無介面的 Fragment,請呼叫帶 tag 參數的多載版本:

add(Fragment fragment, String tag)

由於它沒有畫面,實作時自然不需要(也不該)回傳版面,onCreateView() 可以直接省略。而因為沒有 view ID 可用,tag 字串就成了找回這個 Fragment 的唯一線索,日後一律以 findFragmentByTag() 取得它。

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