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

打造你的 Fragment:從建立類別到加入 Activity

編輯:關於 Android 程式設計

你可以把 Fragment 想成是 Activity 中的一個模組化區塊——它擁有自己的生命週期、接收自己的輸入事件,並且能在 Activity 運作期間被加入或移除(有點像是可在不同 Activity 間重複使用的「子 Activity」)。本文示範如何擴充 Fragment 類別,並搭配 Support Library,讓你的 App 能相容於低至 Android 1.6 的系統版本。

注意:若基於其他理由,你決定 App 的最低 API level 就是 11 以上,那麼就不需要 Support Library,可直接使用框架內建的 Fragment 類別與相關 API。但要留意,本文聚焦於 Support Library 的 API,它在套件簽名上、有時在 API 名稱上,都與平台內建版本略有不同。

建立 Fragment 類別

要建立 Fragment,請擴充 Fragment 類別,然後像處理 Activity 那樣覆寫關鍵的生命週期方法來放入你的 App 邏輯。

建立 Fragment 時有個差異:你必須使用 onCreateView() 回呼來定義版面配置。事實上,這是讓 Fragment 跑起來唯一必需的回呼。以下是一個指定自身版面的簡單 Fragment:

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.ViewGroup;

public class ArticleFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // 為此 Fragment 膨脹版面配置
        return inflater.inflate(R.layout.article_view, container, false);
    }
}

就像 Activity 一樣,Fragment 也應實作其他生命週期回呼,以便在 Fragment 被加入或移出 Activity、以及 Activity 在生命週期狀態間轉換時管理其狀態。舉例來說,當 Activity 的 onPause() 被呼叫時,Activity 中的所有 Fragment 也會收到 onPause() 呼叫。

關於 Fragment 生命週期與回呼方法的更多資訊,請參考 Fragment 開發人員指南。

用 XML 把 Fragment 加入 Activity

雖然 Fragment 是可重複使用、模組化的 UI 元件,但每個 Fragment 類別的實例都必須關聯到一個父 FragmentActivity。你可以在 Activity 的版面 XML 檔中定義每個 Fragment 來建立這種關聯。

注意:FragmentActivity 是 Support Library 提供的特殊 Activity,用來處理 API level 11 以下版本的 Fragment。若你支援的最低系統版本是 API level 11 以上,就可以使用一般的 Activity。

以下是一個版面檔範例,當裝置螢幕被判定為「large」(由目錄名稱的 large 修飾詞指定)時,會把兩個 Fragment 加入 Activity:

res/layout-large/news_articles.xml:

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

    <fragment android:name="com.example.android.fragments.HeadlinesFragment"
              android:id="@+id/headlines_fragment"
              android:layout_weight="1"
              android:layout_width="0dp"
              android:layout_height="match_parent" />

    <fragment android:name="com.example.android.fragments.ArticleFragment"
              android:id="@+id/article_fragment"
              android:layout_weight="2"
              android:layout_width="0dp"
              android:layout_height="match_parent" />

</LinearLayout>

小提醒:關於為不同螢幕尺寸建立版面的更多資訊,可參考「支援不同螢幕尺寸」一文。

以下是 Activity 套用這個版面的方式:

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

public class MainActivity extends FragmentActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.news_articles);
    }
}

注意:當你在版面 XML 檔中定義 Fragment 而把它加入 Activity 時,就無法在執行期移除該 Fragment。若你打算在使用者互動時動態替換 Fragment,就必須在 Activity 首次啟動時才加入 Fragment,做法將於下一課中介紹。

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