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

Android 學習路線(二十一):用 Fragment 打造動態 UI —— 建立一個 Fragment

編輯:關於 Android 程式設計

你可以把 Fragment 想成 Activity 的模組化片段:它有自己的生命週期、能接收自己的輸入事件,也能在 Activity 運行期間被動態加入或移除。因為它是可重複使用的,同一個 Fragment 完全可以在多個 Activity 之間共用,行為上有點像一個「子 Activity」。

提示:若你的應用最低支援 API Level 11 以上,就不需要 Support Library,直接用平台內建的 Fragment API 即可。本課以 Support Library 為主,兩者的差別主要在套件名稱與少數 API 命名。

開始之前,請先確認專案已導入 Support Library。若沒用過,請參考 Support Library Setup 文件設定 v4 庫;你也可以採用 v7 appcompat 庫,它同時提供 ActionBar 支援(相容到 Android 2.1)並包含 Fragment API。

建立一個 Fragment

建立方式與 Activity 類似——繼承 Fragment 類別,覆寫需要的生命週期方法放入你的邏輯。但有個關鍵差異:你必須實作 onCreateView() 來定義版面。實際上只覆寫這一個回呼,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 收到 onPause(),內部所有 Fragment 也會收到同樣的回呼。

完整的生命週期細節,請參考 Fragments 開發指南。

用 XML 在版面中宣告 Fragment

由於 Fragment 是可重用的模組化 UI 元件,每個 Fragment 實例都必須關聯到一個父 FragmentActivity。最簡單的關聯方式,就是直接在 Activity 的 XML 版面檔中定義它。

提示:FragmentActivity 是 Support Library 專為低於 API 11 的系統準備的 Activity 子類。如果你的最低支援版本是 API 11 以上,直接用普通的 Activity 就好。

以下是一個大螢幕版面配置,同時放進兩個 Fragment:

res/layout-large/news_articles.xml

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

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

    <fragment
        android:id="@+id/article_fragment"
        android:name="com.example.android.fragments.ArticleFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2" />
</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);
    }
}

若你採用的是 v7 appcompat 庫,Activity 就該繼承 ActionBarActivity——它是 FragmentActivity 的子類,關於 ActionBar 的設定請參考「設置 Action Bar」那一課。

貼士:不同螢幕尺寸該怎麼配置版面,請參考「支援不同螢幕尺寸」。

一個必須知道的限制

當你在 XML 版面檔裡靜態宣告 Fragment 時,執行期是無法把它移除的。也就是說,這種寫法只適合那些從頭到尾都要留在畫面上的固定區塊。

如果你打算依使用者操作動態切換、隱藏或替換 Fragment,就必須改在 Activity 啟動時用程式碼把它加進去——這正是下一課的主題。

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