Fragments片段

Fragment 表示 Activity 中的行爲或用戶界面部分。您可以將多個片段組合在一個 Activity 中來構建多窗格 UI,以及在多個 Activity 中重複使用某個片段。您可以將片段視爲 Activity 的模塊化組成部分,它具有自己的生命週期,能接收自己的輸入事件,並且您可以在 Activity 運行時添加或移除片段(有點像您可以在不同 Activity 中重複使用的“子 Activity”)。

片段必須始終嵌入在 Activity 中,其生命週期直接受宿主 Activity 生命週期的影響。 例如,當 Activity 暫停時,其中的所有片段也會暫停;當 Activity 被銷燬時,所有片段也會被銷燬。 不過,當 Activity 正在運行(處於已恢復生命週期狀態)時,您可以獨立操縱每個片段,如添加或移除它們。 當您執行此類片段事務時,您也可以將其添加到由 Activity 管理的返回棧 — Activity 中的每個返回棧條目都是一條已發生片段事務的記錄。 返回棧讓用戶可以通過按返回按鈕撤消片段事務(後退)。

當您將片段作爲 Activity 佈局的一部分添加時,它存在於 Activity 視圖層次結構的某個 ViewGroup 內部,並且片段會定義其自己的視圖佈局。您可以通過在 Activity 的佈局文件中聲明片段,將其作爲<fragment> 元素插入您的 Activity 佈局中,或者通過將其添加到某個現有 ViewGroup,利用應用代碼進行插入。不過,片段並非必須成爲 Activity 佈局的一部分;您還可以將沒有自己 UI 的片段用作 Activity 的不可見工作線程。

本文描述如何在開發您的應用時使用片段,包括將片段添加到 Activity 返回棧時如何保持其狀態、如何與 Activity 及 Activity 中的其他片段共享事件、如何爲 Activity 的操作欄發揮作用等等。
一 設計原理
Android 在 Android 3.0(API 級別 11)中引入了片段,主要是爲了給大屏幕(如平板電腦)上更加動態和靈活的 UI 設計提供支持。由於平板電腦的屏幕比手機屏幕大得多,因此可用於組合和交換 UI 組件的空間更大。利用片段實現此類設計時,您無需管理對視圖層次結構的複雜更改。 通過將 Activity 佈局分成片段,您可以在運行時修改 Activity 的外觀,並在由 Activity 管理的返回棧中保留這些更改。
例如,新聞應用可以使用一個片段在左側顯示文章列表,使用另一個片段在右側顯示文章 — 兩個片段並排顯示在一個 Activity 中,每個片段都具有自己的一套生命週期回調方法,並各自處理自己的用戶輸入事件。 因此,用戶不需要使用一個 Activity 來選擇文章,然後使用另一個 Activity 來閱讀文章,而是可以在同一個 Activity 內選擇文章並進行閱讀,如圖 1 中的平板電腦佈局所示。

您應該將每個片段都設計爲可重複使用的模塊化 Activity 組件。也就是說,由於每個片段都會通過各自的生命週期回調來定義其自己的佈局和行爲,您可以將一個片段加入多個 Activity,因此,您應該採用可複用式設計,避免直接從某個片段直接操縱另一個片段。 這特別重要,因爲模塊化片段讓您可以通過更改片段的組合方式來適應不同的屏幕尺寸。 在設計可同時支持平板電腦和手機的應用時,您可以在不同的佈局配置中重複使用您的片段,以根據可用的屏幕空間優化用戶體驗。 例如,在手機上,如果不能在同一 Activity 內儲存多個片段,可能必須利用單獨片段來實現單窗格 UI。

Fragments片段
圖 1. 有關由片段定義的兩個 UI 模塊如何適應不同設計的示例:通過組合成一個 Activity 來適應平板電腦設計,通過單獨片段來適應手機設計。
例如 — 仍然以新聞應用爲例 — 在平板電腦尺寸的設備上運行時,該應用可以在Activity A 中嵌入兩個片段。 不過,在手機尺寸的屏幕上,沒有足以儲存兩個片段的空間,因此Activity A只包括用於顯示文章列表的片段,當用戶選擇文章時,它會啓動Activity B,其中包括用於閱讀文章的第二個片段。 因此,應用可通過重複使用不同組合的片段來同時支持平板電腦和手機,如圖 1 所示。

如需瞭解有關通過利用不同片段組合來適應不同屏幕配置這種方法設計應用的詳細信息,請參閱支持平板電腦和手機指南。
二 創建片段

Fragments片段
圖 2. 片段的生命週期(其 Activity 運行時)。
要想創建片段,您必須創建 Fragment 的子類(或已有其子類)。Fragment 類的代碼與 Activity非常相似。它包含與 Activity 類似的回調方法,如onCreate()、onStart()、onPause() 和 onStop()。實際上,如果您要將現有 Android 應用轉換爲使用片段,可能只需將代碼從 Activity 的回調方法移入片段相應的回調方法中。
通常,您至少應實現以下生命週期方法:
onCreate()
系統會在創建片段時調用此方法。您應該在實現內初始化您想在片段暫停或停止後恢復時保留的必需片段組件。
onCreateView()
系統會在片段首次繪製其用戶界面時調用此方法。 要想爲您的片段繪製 UI,您從此方法中返回的 View 必須是片段佈局的根視圖。如果片段未提供 UI,您可以返回 null。
onPause()
系統將此方法作爲用戶離開片段的第一個信號(但並不總是意味着此片段會被銷燬)進行調用。 您通常應該在此方法內確認在當前用戶會話結束後仍然有效的任何更改(因爲用戶可能不會返回)。
大多數應用都應該至少爲每個片段實現這三個方法,但您還應該使用幾種其他回調方法來處理片段生命週期的各個階段。 處理片段生命週期部分對所有生命週期回調方法做了更詳盡的闡述。
您可能還想擴展幾個子類,而不是 Fragment 基類:

DialogFragment
顯示浮動對話框。使用此類創建對話框可有效地替代使用 Activity 類中的對話框幫助程序方法,因爲您可以將片段對話框納入由 Activity 管理的片段返回棧,從而使用戶能夠返回清除的片段。
ListFragment
顯示由適配器(如 SimpleCursorAdapter)管理的一系列項目,類似於 ListActivity。它提供了幾種管理列表視圖的方法,如用於處理點擊事件的 onListItemClick() 回調。
PreferenceFragment
以列表形式顯示 Preference 對象的層次結構,類似於 PreferenceActivity。這在爲您的應用創建“設置” Activity 時很有用處。
2.1添加用戶界面
片段通常用作 Activity 用戶界面的一部分,將其自己的佈局融入 Activity。
要想爲片段提供佈局,您必須實現 onCreateView() 回調方法,Android 系統會在片段需要繪製其佈局時調用該方法。您對此方法的實現返回的 View 必須是片段佈局的根視圖。
:如果您的片段是 ListFragment 的子類,則默認實現會從 onCreateView() 返回一個 ListView,因此您無需實現它。

要想從 onCreateView() 返回佈局,您可以通過 XML 中定義的佈局資源來擴展布局。爲幫助您執行此操作,onCreateView() 提供了一個 LayoutInflater 對象。

例如,以下這個 Fragment 子類從 example_fragment.xml 文件加載佈局:

public static class ExampleFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.example_fragment, container, false);
    }
}

2.2 創建佈局
在上例中,R.layout.example_fragment 是對應用資源中保存的名爲 example_fragment.xml 的佈局資源的引用。如需瞭解有關如何在 XML 中創建佈局的信息,請參閱用戶界面文檔。

傳遞至 onCreateView() 的 container 參數是您的片段佈局將插入到的父 ViewGroup(來自 Activity 的佈局)。savedInstanceState 參數是在恢復片段時,提供上一片段實例相關數據的 Bundle(處理片段生命週期部分對恢復狀態做了詳細闡述)。

inflate() 方法帶有三個參數

  • 您想要擴展的佈局的資源 ID;
  • 將作爲擴展布局父項的 ViewGroup。傳遞 container 對系統向擴展布局的根視圖(由其所屬的父視圖指定)應用佈局參數具有重要意義;
  • 指示是否應該在擴展期間將擴展布局附加至 ViewGroup(第二個參數)的布爾值。(在本例中,其值爲 false,因爲系統已經將擴展布局插入 container — 傳遞 true 值會在最終佈局中創建一個多餘的視圖組。)
    現在,您已經瞭解瞭如何創建提供佈局的片段。接下來,您需要將該片段添加到您的 Activity 中。
    2.3 向 Activity 添加片段
    通常,片段向宿主 Activity 貢獻一部分 UI,作爲 Activity 總體視圖層次結構的一部分嵌入到 Activity 中。可以通過兩種方式向 Activity 佈局添加片段:
  • 在 Activity 的佈局文件內聲明片段

在本例中,您可以將片段當作視圖來爲其指定佈局屬性。 例如,以下是一個具有兩個片段的 Activity 的佈局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://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> 中的 android:name 屬性指定要在佈局中實例化的 Fragment 類。
當系統創建此 Activity 佈局時,會實例化在佈局中指定的每個片段,併爲每個片段調用onCreateView() 方法,以檢索每個片段的佈局。系統會直接插入片段返回的 View 來替代<fragment> 元素。
注:每個片段都需要一個唯一的標識符,重啓 Activity 時,系統可以使用該標識符來恢復片段(您也可以使用該標識符來捕獲片段以執行某些事務,如將其移除)。 可以通過三種方式爲片段提供 ID:

  • 爲 android:id 屬性提供唯一 ID。
  • 爲 android:tag 屬性提供唯一字符串。
  • 如果您未給以上兩個屬性提供值,系統會使用容器視圖的 ID。
    或者通過編程方式將片段添加到某個現有 ViewGroup
    您可以在 Activity 運行期間隨時將片段添加到 Activity 佈局中。您只需指定要將片段放入哪個 ViewGroup。

要想在您的 Activity 中執行片段事務(如添加、移除或替換片段),您必須使用FragmentTransaction 中的 API。您可以像下面這樣從 Activity 獲取一個 FragmentTransaction 實例:

FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

然後,您可以使用 add() 方法添加一個片段,指定要添加的片段以及將其插入哪個視圖。例如:

ExampleFragment fragment = new ExampleFragment();
fragmentTransaction.add(R.id.fragment_container, fragment);
fragmentTransaction.commit();

傳遞到 add() 的第一個參數是 ViewGroup,即應該放置片段的位置,由資源 ID 指定,第二個參數是要添加的片段。
一旦您通過 FragmentTransaction 做出了更改,就必須調用 commit() 以使更改生效。
添加沒有 UI 的片段
上例展示瞭如何向您的 Activity 添加片段以提供 UI。不過,您還可以使用片段爲 Activity 提供後臺行爲,而不顯示額外 UI。
要想添加沒有 UI 的片段,請使用 add(Fragment, String) 從 Activity 添加片段(爲片段提供一個唯一的字符串“標記”,而不是視圖 ID)。 這會添加片段,但由於它並不與 Activity 佈局中的視圖關聯,因此不會收到對 onCreateView() 的調用。因此,您不需要實現該方法。

並非只能爲非 UI 片段提供字符串標記 — 您也可以爲具有 UI 的片段提供字符串標記 — 但如果片段沒有 UI,則字符串標記將是標識它的唯一方式。如果您想稍後從 Activity 中獲取片段,則需要使用findFragmentByTag()。

如需查看將沒有 UI 的片段用作後臺工作線程的示例 Activity,請參閱 FragmentRetainInstance.java示例,該示例包括在 SDK 示例(通過 Android SDK 管理器提供)中,以<sdk_root>/APIDemos/app/src/main/java/com/example/android/apis/app/FragmentRetainInstance.java 形式位於您的系統中。

三 管理片段
要想管理您的 Activity 中的片段,您需要使用 FragmentManager。要想獲取它,請從您的 Activity 調用getFragmentManager()。
您可以使用 FragmentManager 執行的操作包括

  • 通過 findFragmentById()(對於在 Activity 佈局中提供 UI 的片段)或 findFragmentByTag()(對於提供或不提供 UI 的片段)獲取 Activity 中存在的片段
  • 通過 popBackStack()(模擬用戶發出的返回命令)將片段從返回棧中彈出。
  • 通過 addOnBackStackChangedListener() 註冊一個偵聽返回棧變化的偵聽器。
    如需瞭解有關這些方法以及其他方法的詳細信息,請參閱 FragmentManager 類文檔。

如上文所示,您也可以使用 FragmentManager 打開一個 FragmentTransaction,通過它來執行某些事務,如添加和移除片段。
四 執行片段事務

在 Activity 中使用片段的一大優點是,可以根據用戶行爲通過它們執行添加、移除、替換以及其他操作。 您提交給 Activity 的每組更改都稱爲事務,您可以使用 FragmentTransaction 中的 API 來執行一項事務。您也可以將每個事務保存到由 Activity 管理的返回棧內,從而讓用戶能夠回退片段更改(類似於回退 Activity)

您可以像下面這樣從 FragmentManager 獲取一個 FragmentTransaction 實例:

FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

每個事務都是您想要同時執行的一組更改。您可以使用 add()、remove() 和 replace() 等方法爲給定事務設置您想要執行的所有更改。然後,要想將事務應用到 Activity,您必須調用 commit()。

不過,在您調用 commit() 之前,您可能想調用 addToBackStack(),以將事務添加到片段事務返回棧。 該返回棧由 Activity 管理,允許用戶通過按返回按鈕返回上一片段狀態。

例如,以下示例說明了如何將一個片段替換成另一個片段,以及如何在返回棧中保留先前狀態:

// Create new fragment and transaction
Fragment newFragment = new ExampleFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();

// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);

// Commit the transaction
transaction.commit();

在上例中,newFragment 會替換目前在 R.id.fragment_container ID 所標識的佈局容器中的任何片段(如有)。通過調用 addToBackStack() 可將替換事務保存到返回棧,以便用戶能夠通過按返回按鈕撤消事務並回退到上一片段。

如果您向事務添加了多個更改(如又一個 add() 或 remove()),並且調用了 addToBackStack(),則在調用 commit() 前應用的所有更改都將作爲單一事務添加到返回棧,並且返回按鈕會將它們一併撤消。
向 FragmentTransaction 添加更改的順序無關緊要,不過:

  • 您必須最後調用 commit()
  • 如果您要向同一容器添加多個片段,則您添加片段的順序將決定它們在視圖層次結構中的出現順序
    如果您沒有在執行移除片段的事務時調用 addToBackStack(),則事務提交時該片段會被銷燬,用戶將無法回退到該片段。 不過,如果您在刪除片段時調用了 addToBackStack(),則系統會停止該片段,並在用戶回退時將其恢復。
    提示:對於每個片段事務,您都可以通過在提交前調用 setTransition() 來應用過渡動畫。
    調用 commit() 不會立即執行事務,而是在 Activity 的 UI 線程(“主”線程)可以執行該操作時再安排其在線程上運行。不過,如有必要,您也可以從 UI 線程調用 executePendingTransactions() 以立即執行 commit() 提交的事務。通常不必這樣做,除非其他線程中的作業依賴該事務。

注意:您只能在 Activity 保存其狀態(用戶離開 Activity)之前使用 commit() 提交事務。如果您試圖在該時間點後提交,則會引發異常。 這是因爲如需恢復 Activity,則提交後的狀態可能會丟失。 對於丟失提交無關緊要的情況,請使用 commitAllowingStateLoss()。

五 與 Activity 通信
儘管 Fragment 是作爲獨立於 Activity 的對象實現,並且可在多個 Activity 內使用,但片段的給定實例會直接綁定到包含它的 Activity。

具體地說,片段可以通過 getActivity() 訪問 Activity 實例,並輕鬆地執行在 Activity 佈局中查找視圖等任務。

View listView = getActivity().findViewById(R.id.list);

同樣地,您的 Activity 也可以使用 findFragmentById() 或 findFragmentByTag(),通過從 FragmentManager 獲取對 Fragment 的引用來調用片段中的方法。例如:

ExampleFragment fragment = (ExampleFragment) getFragmentManager().findFragmentById(R.id.example_fragment);

5.1 創建對 Activity 的事件回調
在某些情況下,您可能需要通過片段與 Activity 共享事件。執行此操作的一個好方法是,在片段內定義一個回調接口,並要求宿主 Activity 實現它。 當 Activity 通過該接口收到回調時,可以根據需要與佈局中的其他片段共享這些信息。

例如,如果一個新聞應用的 Activity 有兩個片段 — 一個用於顯示文章列表(片段 A),另一個用於顯示文章(片段 B)— 那麼片段 A 必須在列表項被選定後告知 Activity,以便它告知片段 B 顯示該文章。 在本例中,OnArticleSelectedListener 接口在片段 A 內聲明:

public static class FragmentA extends ListFragment {
    ...
    // Container Activity must implement this interface
    public interface OnArticleSelectedListener {
        public void onArticleSelected(Uri articleUri);
    }
    ...
}

然後,該片段的宿主 Activity 會實現 OnArticleSelectedListener 接口並替代 onArticleSelected(),將來自片段 A 的事件通知片段 B。爲確保宿主 Activity 實現此接口,片段 A 的 onAttach() 回調方法(系統在向 Activity 添加片段時調用的方法)會通過轉換傳遞到 onAttach() 中的 Activity 來實例化 OnArticleSelectedListener 的實例:

public static class FragmentA extends ListFragment {
    OnArticleSelectedListener mListener;
    ...
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mListener = (OnArticleSelectedListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString() + " must implement OnArticleSelectedListener");
        }
    }
    ...
}

如果 Activity 未實現接口,則片段會引發 ClassCastException。實現時,mListener 成員會保留對 Activity 的 OnArticleSelectedListener 實現的引用,以便片段 A 可以通過調用OnArticleSelectedListener 接口定義的方法與 Activity 共享事件。例如,如果片段 A 是ListFragment 的一個擴展,則用戶每次點擊列表項時,系統都會調用片段中的 onListItemClick(),然後該方法會調用 onArticleSelected() 以與 Activity 共享事件:

public static class FragmentA extends ListFragment {
    OnArticleSelectedListener mListener;
    ...
    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        // Append the clicked item's row ID with the content provider Uri
        Uri noteUri = ContentUris.withAppendedId(ArticleColumns.CONTENT_URI, id);
        // Send the event and Uri to the host activity
        mListener.onArticleSelected(noteUri);
    }
    ...
}

傳遞到 onListItemClick() 的 id 參數是被點擊項的行 ID,即 Activity(或其他片段)用來從應用的 ContentProvider 獲取文章的 ID。
5.2 嚮應用欄添加項目
您的片段可以通過實現 onCreateOptionsMenu() 向 Activity 的選項菜單(並因此嚮應用欄)貢獻菜單項。不過,爲了使此方法能夠收到調用,您必須在 onCreate() 期間調用 setHasOptionsMenu(),以指示片段想要向選項菜單添加菜單項(否則,片段將不會收到對 onCreateOptionsMenu() 的調用)。

您之後從片段添加到選項菜單的任何菜單項都將追加到現有菜單項之後。 選定菜單項時,片段還會收到對 onOptionsItemSelected() 的回調。

您還可以通過調用 registerForContextMenu(),在片段佈局中註冊一個視圖來提供上下文菜單。用戶打開上下文菜單時,片段會收到對 onCreateContextMenu() 的調用。當用戶選擇某個菜單項時,片段會收到對 onContextItemSelected() 的調用。

:儘管您的片段會收到與其添加的每個菜單項對應的菜單項選定回調,但當用戶選擇菜單項時,Activity 會首先收到相應的回調。 如果 Activity 對菜單項選定回調的實現不會處理選定的菜單項,則系統會將事件傳遞到片段的回調。 這適用於選項菜單和上下文菜單。
六 處理片段生命週期
Fragments片段
圖 3. Activity 生命週期對片段生命週期的影響。
管理片段生命週期與管理 Activity 生命週期很相似。和 Activity 一樣,片段也以三種狀態存在:
繼續
片段在運行中的 Activity 中可見。
暫停
另一個 Activity 位於前臺並具有焦點,但此片段所在的 Activity 仍然可見(前臺 Activity 部分透明,或未覆蓋整個屏幕)。
停止
片段不可見。宿主 Activity 已停止,或片段已從 Activity 中移除,但已添加到返回棧。 停止片段仍然處於活動狀態(系統會保留所有狀態和成員信息)。 不過,它對用戶不再可見,如果 Activity 被終止,它也會被終止。

同樣與 Activity 一樣,假使 Activity 的進程被終止,而您需要在重建 Activity 時恢復片段狀態,您也可以使用 Bundle 保留片段的狀態。您可以在片段的 onSaveInstanceState() 回調期間保存狀態,並可在 onCreate()、onCreateView()或 onActivityCreated() 期間恢復狀態。

Activity 生命週期與片段生命週期之間的最顯著差異在於它們在其各自返回棧中的存儲方式。 默認情況下,Activity 停止時會被放入由系統管理的 Activity 返回棧(以便用戶通過返回按鈕回退到 Activity,任務和返回棧對此做了闡述)。不過,僅當您在移除片段的事務執行期間通過調用 addToBackStack() 顯式請求保存實例時,系統纔會將片段放入由宿主 Activity 管理的返回棧。

在其他方面,管理片段生命週期與管理 Activity 生命週期非常相似。 因此,管理 Activity 生命週期的做法同樣適用於片段。 但您還需要了解 Activity 的生命週期對片段生命週期的影響。

注意:如需 Fragment 內的某個 Context 對象,可以調用 getActivity()。但要注意,請僅在片段附加到 Activity 時調用 getActivity()。如果片段尚未附加,或在其生命週期結束期間分離,則 getActivity()將返回 null。

6.1 與 Activity 生命週期協調一致
片段所在的 Activity 的生命週期會直接影響片段的生命週期,其表現爲,Activity 的每次生命週期回調都會引發每個片段的類似回調。 例如,當 Activity 收到 onPause() 時,Activity 中的每個片段也會收到onPause()。

不過,片段還有幾個額外的生命週期回調,用於處理與 Activity 的唯一交互,以執行構建和銷燬片段 UI 等操作。 這些額外的回調方法是:
onAttach()
在片段已與 Activity 關聯時調用(Activity 傳遞到此方法內)。onCreateView()
調用它可創建與片段關聯的視圖層次結構。
onActivityCreated()
在 Activity 的 onCreate() 方法已返回時調用。
onDestroyView()
在移除與片段關聯的視圖層次結構時調用。onDetach()在取消片段與 Activity 的關聯時調用。
圖 3 圖示說明了受其宿主 Activity 影響的片段生命週期流。在該圖中,您可以看到 Activity 的每個連續狀態如何決定片段可以收到的回調方法。 例如,當 Activity 收到其 onCreate() 回調時,Activity 中的片段只會收到 onActivityCreated() 回調。

一旦 Activity 達到恢復狀態,您就可以隨意向 Activity 添加片段和移除其中的片段。 因此,只有當 Activity 處於恢復狀態時,片段的生命週期才能獨立變化。
不過,當 Activity 離開恢復狀態時,片段會在 Activity 的推動下再次經歷其生命週期。
七 示例
爲了將本文闡述的所有內容融會貫通,以下提供了一個示例,其中的 Activity 使用兩個片段來創建一個雙窗格佈局。 下面的 Activity 包括兩個片段:一個用於顯示莎士比亞戲劇標題列表,另一個用於從列表中選定戲劇時顯示其摘要。 此外,它還展示瞭如何根據屏幕配置提供不同的片段配置。

注:FragmentLayout.java 中提供了此 Activity 的完整源代碼
主 Activity 會在 onCreate() 期間以常規方式應用佈局:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.fragment_layout);
}

應用的佈局爲 fragment_layout.xml:

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

    <fragment class="com.example.android.apis.app.FragmentLayout$TitlesFragment"
            android:id="@+id/titles" android:layout_weight="1"
            android:layout_width="0px" android:layout_height="match_parent" />

    <FrameLayout android:id="@+id/details" android:layout_weight="1"
            android:layout_width="0px" android:layout_height="match_parent"
            android:background="?android:attr/detailsElementBackground" />

</LinearLayout>

通過使用此佈局,系統可在 Activity 加載佈局時立即實例化 TitlesFragment(列出戲劇標題),而FrameLayout(用於顯示戲劇摘要的片段所在位置)則會佔用屏幕右側的空間,但最初處於空白狀態。 正如您將在下文所見的那樣,用戶從列表中選擇某個項目後,系統纔會將片段放入 FrameLayout
不過,並非所有屏幕配置都具有足夠的寬度,可以並排顯示戲劇列表和摘要。 因此,以上佈局僅用於橫向屏幕配置(佈局保存在 res/layout-land/fragment_layout.xml)。
因此,當屏幕縱向顯示時,系統會應用以下佈局(保存在 res/layout/fragment_layout.xml):

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">
    <fragment class="com.example.android.apis.app.FragmentLayout$TitlesFragment"
            android:id="@+id/titles"
            android:layout_width="match_parent" android:layout_height="match_parent" />
</FrameLayout>

此佈局僅包括 TitlesFragment。這意味着,當設備縱向顯示時,只有戲劇標題列表可見。 因此,當用戶在此配置中點擊某個列表項時,應用會啓動一個新 Activity 來顯示摘要,而不是加載另一個片段。
接下來,您可以看到如何在片段類中實現此目的。第一個片段是 TitlesFragment,它顯示莎士比亞戲劇標題列表。該片段擴展了 ListFragment,並依靠它來處理大多數列表視圖工作。

當您檢查此代碼時,請注意,用戶點擊列表項時可能會出現兩種行爲:系統可能會創建並顯示一個新片段,從而在同一 Activity 中顯示詳細信息(將片段添加到 FrameLayout),也可能會啓動一個新 Activity(在該 Activity 中可顯示片段),具體取決於這兩個佈局中哪一個處於活動狀態。

public static class TitlesFragment extends ListFragment {
    boolean mDualPane;
    int mCurCheckPosition = 0;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        // Populate list with our static array of titles.
        setListAdapter(new ArrayAdapter<String>(getActivity(),
                android.R.layout.simple_list_item_activated_1, Shakespeare.TITLES));

        // Check to see if we have a frame in which to embed the details
        // fragment directly in the containing UI.
        View detailsFrame = getActivity().findViewById(R.id.details);
        mDualPane = detailsFrame != null && detailsFrame.getVisibility() == View.VISIBLE;

        if (savedInstanceState != null) {
            // Restore last state for checked position.
            mCurCheckPosition = savedInstanceState.getInt("curChoice", 0);
        }

        if (mDualPane) {
            // In dual-pane mode, the list view highlights the selected item.
            getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
            // Make sure our UI is in the correct state.
            showDetails(mCurCheckPosition);
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt("curChoice", mCurCheckPosition);
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        showDetails(position);
    }

    /**
     * Helper function to show the details of a selected item, either by
     * displaying a fragment in-place in the current UI, or starting a
     * whole new activity in which it is displayed.
     */
    void showDetails(int index) {
        mCurCheckPosition = index;

        if (mDualPane) {
            // We can display everything in-place with fragments, so update
            // the list to highlight the selected item and show the data.
            getListView().setItemChecked(index, true);

            // Check what fragment is currently shown, replace if needed.
            DetailsFragment details = (DetailsFragment)
                    getFragmentManager().findFragmentById(R.id.details);
            if (details == null || details.getShownIndex() != index) {
                // Make new fragment to show this selection.
                details = DetailsFragment.newInstance(index);

                // Execute a transaction, replacing any existing fragment
                // with this one inside the frame.
                FragmentTransaction ft = getFragmentManager().beginTransaction();
                if (index == 0) {
                    ft.replace(R.id.details, details);
                } else {
                    ft.replace(R.id.a_item, details);
                }
                ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
                ft.commit();
            }

        } else {
            // Otherwise we need to launch a new activity to display
            // the dialog fragment with selected text.
            Intent intent = new Intent();
            intent.setClass(getActivity(), DetailsActivity.class);
            intent.putExtra("index", index);
            startActivity(intent);
        }
    }
}

第二個片段 DetailsFragment 顯示從 TitlesFragment 的列表中選擇的項目的戲劇摘要:

public static class DetailsFragment extends Fragment {
    /**
     * Create a new instance of DetailsFragment, initialized to
     * show the text at 'index'.
     */
    public static DetailsFragment newInstance(int index) {
        DetailsFragment f = new DetailsFragment();

        // Supply index input as an argument.
        Bundle args = new Bundle();
        args.putInt("index", index);
        f.setArguments(args);

        return f;
    }

    public int getShownIndex() {
        return getArguments().getInt("index", 0);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        if (container == null) {
            // We have different layouts, and in one of them this
            // fragment's containing frame doesn't exist.  The fragment
            // may still be created from its saved state, but there is
            // no reason to try to create its view hierarchy because it
            // won't be displayed.  Note this is not needed -- we could
            // just run the code below, where we would create and return
            // the view hierarchy; it would just never be used.
            return null;
        }

        ScrollView scroller = new ScrollView(getActivity());
        TextView text = new TextView(getActivity());
        int padding = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                4, getActivity().getResources().getDisplayMetrics());
        text.setPadding(padding, padding, padding, padding);
        scroller.addView(text);
        text.setText(Shakespeare.DIALOGUE[getShownIndex()]);
        return scroller;
    }
}

從 TitlesFragment 類中重新調用,如果用戶點擊某個列表項,且當前佈局“根本不”

包括 R.id.details 視圖(即 DetailsFragment 所屬視圖),則應用會啓動 DetailsActivity Activity 以顯示該項目的內容。

以下是 DetailsActivity,它簡單地嵌入了 DetailsFragment,以在屏幕爲縱向時顯示所選的戲劇摘要:

public static class DetailsActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (getResources().getConfiguration().orientation
                == Configuration.ORIENTATION_LANDSCAPE) {
            // If the screen is now in landscape mode, we can show the
            // dialog in-line with the list so we don't need this activity.
            finish();
            return;
        }

        if (savedInstanceState == null) {
            // During initial setup, plug in the details fragment.
            DetailsFragment details = new DetailsFragment();
            details.setArguments(getIntent().getExtras());
            getFragmentManager().beginTransaction().add(android.R.id.content, details).commit();
        }
    }
}

請注意,如果配置爲橫向,則此 Activity 會自行完成,以便主 Activity 可以接管並沿 TitlesFragment顯示 DetailsFragment。如果用戶在縱向顯示時啓動 DetailsActivity,但隨後旋轉爲橫向(這會重啓當前 Activity),就可能出現這種情況。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章