介紹了這么多抽象的東西,也是時候應該學習一下碎片的具體用法了。你已經知道,碎 片通常都是在平板開發中才會使用的,因此我們首先要做的就是新建一個平板電腦的模擬 器。由於 4.0 系統的平板模擬器好像存在 bug,這里我就新建一個 4.2 系統的平板模擬器,如 圖 4.4 所示。

圖 4.4
現在啟動這個平板模擬器,效果如圖 4.5 所示。

圖 4.5
好了,准備工作都完成了,接着新建一個 FragmentTest 項目,然后開始我們的碎片探索
之旅吧。
4.2.1 碎片的簡單用法
這里我們准備先寫一個最簡單的碎片示例來練練手,在一個活動當中添加兩個碎片,並 讓這兩個碎片平分活動空間。
新建一個左側碎片布局 left_fragment.xml,代碼如下所示:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="vertical" >
<Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="Button"
/>
</LinearLayout>
這個布局非常簡單,只放置了一個按鈕,並讓它水平居中顯示。然后新建右側碎片布局
right_fragment.xml,代碼如下所示:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"
android:background="#00ff00" android:orientation="vertical" >
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:textSize="20sp"
android:text="This is right fragment"
/>
</LinearLayout>
可以看到,我們將這個布局的背景色設置成綠色,並放置了一個 TextView 用於顯示一
段文本。接着新建一個 LeftFragment 類,繼承自 Fragment。注意,這里可能會有兩個不同包 下的 Fragment 供你選擇,建議使用 android.app.Fragment,因為我們的程序是面向 Android 4.0 以上系統的,另一個包下的 Fragment 主要是用於兼容低版本的 Android 系統。LeftFragment 的代碼如下所示:
public class LeftFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.left_fragment, container, false);
return view;
}
}
這里僅僅是重寫了 Fragment 的 onCreateView()方法,然后在這個方法中通過 LayoutInflater 的 inflate()方法將剛才定義的 left_fragment 布局動態加載進來,整個方法簡單明了。接着我 們用同樣的方法再新建一個 RightFragment,代碼如下所示:
public class RightFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.right_fragment, container, false);
return view;
}
}
基本上代碼都是相同的,相信已經沒有必要再做什么解釋了。接下來修改 activity_main.xml
中的代碼,如下所示:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" >
<fragment android:id="@+id/left_fragment"
android:name="com.example.fragmenttest.LeftFragment" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" />
<fragment android:id="@+id/right_fragment" android:name="com.example.fragmenttest.RightFragment" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" />
</LinearLayout>
可以看到,我們使用了<fragment>標簽在布局中添加碎片,其中指定的大多數屬性你都 是熟悉的,只不過這里還需要通過 android:name 屬性來顯式指明要添加的碎片類名,注意一 定要將類的包名也加上。
這樣最簡單的碎片示例就已經寫好了,現在運行一下程序,效果如圖 4.6 所示。

圖 4.6
正如我們所期待的一樣,兩個碎片平分了整個活動的布局。不過這個例子實在是太簡單 了,在真正的項目中很難有什么實際的作用,因此我們馬上來看一看,關於碎片更加高級的 使用技巧。
4.2.2 動態添加碎片
在上一節當中,你已經學會了在布局文件中添加碎片的方法,不過碎片真正的強大之處 在於,它可以在程序運行時動態地添加到活動當中。根據具體情況來動態地添加碎片,你就 可以將程序界面定制得更加多樣化。
我們還是在上一節代碼的基礎上繼續完善,新建 another_right_fragment.xml,代碼如下 所示:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"
android:background="#ffff00" android:orientation="vertical" >
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:textSize="20sp"
android:text="This is another right fragment"
/>
</LinearLayout>
這個布局文件的代碼和 right_fragment.xml 中的代碼基本相同,只是將背景色改成了黃 色,並將顯示的文字改了改。然后新建 AnotherRightFragment 作為另一個右側碎片,代碼如 下所示:
public class AnotherRightFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.another_right_fragment,
container, false);
return view;
}
}
代碼同樣非常簡單,在 onCreateView()方法中加載了剛剛創建的 another_right_fragment
布局。這樣我們就准備好了另一個碎片,接下來看一下如何將它動態地添加到活動當中。修
改 activity_main.xml,代碼如下所示:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent">
<fragment android:id="@+id/left_fragment" android:name="com.example.fragmenttest.LeftFragment" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" />
<FrameLayout android:id="@+id/right_layout" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" >
<fragment android:id="@+id/right_fragment" android:name="com.example.fragmenttest.RightFragment" android:layout_width="match_parent" android:layout_height="match_parent" />
</FrameLayout>
</LinearLayout>
可以看到,現在將右側碎片放在了一個 FrameLayout 中,還記得這個布局嗎?在上一章 中我們學過,這是 Android 中最簡單的一種布局,它沒有任何的定位方式,所有的控件都會 擺放在布局的左上角。由於這里僅需要在布局里放入一個碎片,因此非常適合使用 FrameLayout。
之后我們將在代碼中替換 FrameLayout 里的內容,從而實現動態添加碎片的功能。修改
MainActivity 中的代碼,如下所示:
public class MainActivity extends Activity implements OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button:
AnotherRightFragment fragment = new AnotherRightFragment(); FragmentManager fragmentManager = getFragmentManager(); FragmentTransaction transaction = fragmentManager.
beginTransaction();
transaction.replace(R.id.right_layout, fragment);
transaction.commit();
break;
default:
break;
}
}
}
可以看到,首先我們給左側碎片中的按鈕注冊了一個點擊事件,然后將動態添加碎片的 邏輯都放在了點擊事件里進行。結合代碼可以看出,動態添加碎片主要分為 5 步。
1. 創建待添加的碎片實例。
2. 獲取到 FragmentManager,在活動中可以直接調用 getFragmentManager()方法得到。
3. 開啟一個事務,通過調用 beginTransaction()方法開啟。
4. 向容器內加入碎片,一般使用 replace()方法實現,需要傳入容器的 id 和待添加的碎 片實例。
5. 提交事務,調用 commit()方法來完成。 這樣就完成了在活動中動態添加碎片的功能,重新運行程序,可以看到和之前相同的界
面,然后點擊一下按鈕,效果如圖 4.7 所示。

圖 4.7
4.2.3 在碎片中模擬返回棧
在上一小節中,我們成功實現了向活動中動態添加碎片的功能,不過你嘗試一下就會發 現,通過點擊按鈕添加了一個碎片之后,這時按下 Back 鍵程序就會直接退出。如果這里我 們想模仿類似於返回棧的效果,按下 Back 鍵可以回到上一個碎片,該如何實現呢?
其實很簡單,FragmentTransaction 中提供了一個 addToBackStack()方法,可以用於將一 個事務添加到返回棧中,修改 MainActivity 中的代碼,如下所示:
public class MainActivity extends Activity implements OnClickListener {
……
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button:
AnotherRightFragment fragment = new AnotherRightFragment(); FragmentManager fragmentManager = getFragmentManager(); FragmentTransaction transaction = fragmentManager.
beginTransaction();
transaction.replace(R.id.right_layout, fragment); transaction.addToBackStack(null); transaction.commit();
break;
default:
break;
}
}
}
這里我們在事務提交之前調用了 FragmentTransaction 的 addToBackStack()方法,它可以 接收一個名字用於描述返回棧的狀態,一般傳入 null 即可。現在重新運行程序,並點擊按鈕 將 AnotherRightFragment 添加到活動中,然后按下 Back 鍵,你會發現程序並沒有退出,而 是回到了 RightFragment 界面,再次按下 Back 鍵程序才會退出。
4.2.4 碎片和活動之間進行通信
雖然碎片都是嵌入在活動中顯示的,可是實際上它們的關系並沒有那么親密。你可以看 出,碎片和活動都是各自存在於一個獨立的類當中的,它們之間並沒有那么明顯的方式來直 接進行通信。如果想要在活動中調用碎片里的方法,或者在碎片中調用活動里的方法,應該 如何實現呢?
為了方便碎片和活動之間進行通信,FragmentManager 提供了一個類似於 findViewById()
的方法,專門用於從布局文件中獲取碎片的實例,代碼如下所示:
RightFragment rightFragment = (RightFragment) getFragmentManager()
.findFragmentById(R.id.right_fragment);
調用 FragmentManager 的 findFragmentById()方法,可以在活動中得到相應碎片的實例, 然后就能輕松地調用碎片里的方法了。
掌握了如何在活動中調用碎片里的方法,那在碎片中又該怎樣調用活動里的方法呢?其 實這就更簡單了,在每個碎片中都可以通過調用 getActivity()方法來得到和當前碎片相關聯 的活動實例,代碼如下所示:
MainActivity activity = (MainActivity) getActivity();
有了活動實例之后,在碎片中調用活動里的方法就變得輕而易舉了。另外當碎片中需要 使用 Context 對象時,也可以使用 getActivity()方法,因為獲取到的活動本身就是一個 Context 對象了。
這時不知道你心中會不會產生一個疑問,既然碎片和活動之間的通信問題已經解決了, 那么碎片和碎片之間可不可以進行通信呢?
說實在的,這個問題並沒有看上去的復雜,它的基本思路非常簡單,首先在一個碎片中 可以得到與它相關聯的活動,然后再通過這個活動去獲取另外一個碎片的實例,這樣也就實 現了不同碎片之間的通信功能,因此這里我們的答案是肯定的。
