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

Android Fragment 動態創建詳解:從靜態聲明到執行期替換

編輯:關於 Android 程式設計

Fragment 是什麼

Fragment 可以理解為 Activity 介面中的一個片段,或是一種行為單元。把多個 Fragment 組合成一個 Activity,就能拼出多畫面的介面;而同一個 Fragment 也能在不同 Activity 之間複用。本質上,它是 Activity 的一段模組化程式碼,擁有自己的生命週期,能接收自己的事件,並在 Activity 執行過程中動態加入或移除。

Fragment 無法獨立存在,必須依附在 Activity 之上,其生命週期直接受宿主 Activity 影響:Activity 暫停時,它擁有的所有 Fragment 一起暫停;Activity 銷毀時,Fragment 也隨之銷毀。反過來說,當 Activity 處於執行狀態(onResume 之後、onPause 之前),你可以針對每個 Fragment 單獨操作,例如加入或刪除。

這些針對 Fragment 的操作被稱為「事務」(Transaction),由 Activity 管理並可放入返回堆疊。堆疊中的每一筆就是一 Fragment 的一次事務,有了它,就能反向回退這些操作,從而在 Fragment 層級支援返回鍵的導航行為。

把 Fragment 加入 Activity 時,它必須置於某個 ViewGroup 容器中並定義自己的介面。宣告方式有兩種:在 layout.xml 中以 <fragment> 標籤靜態聲明,或在程式碼中建立後加入 ViewGroup。值得一提的是,Fragment 也可以完全不顯示介面,隱身在背景為 Activity 工作。

實戰一:靜態聲明

先在專案的佈局檔中放入兩個 Fragment:

 <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" tools:context=".MainActivity" > <fragment android:id="@+id/fragment1" android:name="com.wuyudong.fragment.Fragment1" android:layout_width="0dip" android:layout_height="fill_parent" android:layout_weight="1" > </fragment> <fragment android:id="@+id/fragment2" android:name="com.wuyudong.fragment.Fragment2" android:layout_width="0dip" android:layout_height="fill_parent" android:layout_weight="1" > </fragment> </LinearLayout> 

接著在 layout 資料夾下新增各自的佈局檔。

fragment1.xml

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#0000ff" android:orientation="vertical" > </LinearLayout> 

fragment2.xml

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ff00" android:orientation="vertical" > </LinearLayout> 

然後在原始碼資料夾下建立對應的兩個 Java 檔。

Fragment1.java

 public class Fragment1 extends Fragment { /** * 當fragment被創建的時候,調用的方法,返回當前fragment顯示的內容 */ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment1, null); } } 

Fragment2.java

 public class Fragment2 extends Fragment { /** * 當fragment被創建的時候,調用的方法,返回當前fragment顯示的內容 */ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment2, null); } } 

執行專案,可以看到左右並排顯示兩個不同顏色的區塊:

實戰二:動態創建

接下來改成執行期決定要放哪一個 Fragment。做法很簡單:把 activity_main.xml 裡的兩個 <fragment> 標籤刪掉,其餘不動,然後在 MainActivity 中補上這段程式碼。

 public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 1、判斷當前手機的朝向 int width = getWindowManager().getDefaultDisplay().getWidth(); int height = getWindowManager().getDefaultDisplay().getHeight(); Fragment1 fragment1 = new Fragment1(); Fragment2 fragment2 = new Fragment2(); FragmentManager fm = getFragmentManager(); FragmentTransaction ft = fm.beginTransaction(); if (width > height) { // 水平方向 ft.replace(android.R.id.content, fragment1); } else { ft.replace(android.R.id.content, fragment2); } ft.commit(); } } 

這段程式碼的效果是:依手機當前朝向不同,顯示不同的 Fragment。

動態操作的三個關鍵步驟

  • 取得 FragmentManager,透過 beginTransaction() 開啟一筆事務。
  • 使用 replace、add、remove 等方法描述這次要做的變更,容器通常傳 android.R.id.content 表示整個介面。
  • 呼叫 commit() 提交。少了這一步,畫面不會有任何變化。

如果希望使用者能按返回鍵回退這次切換,只要在 commit 之前多呼叫一句 addToBackStack(null) 即可。

以上就是 Fragment 動態創建的完整示範,希望能幫助到你。

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