Fragment是作為Activity的UI的一部分,它內嵌在Activity中,多個Fragment可以把一個Activity分成多個部分,這在大屏幕手機或者平板電腦中會比較多的用到,這樣就不用使用多個Activity來切換這么麻煩了。當然Fragment也可以不顯示,只在后台處理一些數據,這篇文章中就暫時不談到這個。以下來看怎么靜態地在Activity的布局文件中添加Fragment.
自定義的Fragment通常要繼承Fragment這個類,也有一些特殊的是繼承ListFragment,DialogFragment等。繼承Fragment類通常要實現三個方法:onCreate(), onCreateView(), onPause();
我在Activity中定義了兩個Fragment,一個是放在左邊的LeftFragment,一個是放在右邊的RightFragment.以下是代碼:首先我們要實現自己的Fragment類
LeftFragment類:
public class LeftFragment extends Fragment
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
System.out.println("LeftFragment onCreate");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
System.out.println("LeftFragment onCreateView");
// 第一個參數是這個Fragment將要顯示的界面布局,第二個參數是這個Fragment所屬的Activity,第三個參數是決定此fragment是否附屬於Activity
return inflater.inflate(R.layout.leftfragment, container, true);
}
@Override
public void onResume()
{
super.onResume();
System.out.println("LeftFragment onResume");
}
@Override
public void onPause()
{
super.onPause();
System.out.println("LeftFragment onPause");
}
@Override
public void onStop()
{
super.onStop();
System.out.println("LeftFragment onStop");
}
}
這里實現了幾種回調函數,主要是為了看清Activity和Fragment生命周期之間的關系.其中onCreateView()方法是將本Fragment對應的布局返回給Activity的布局,讓Activity進行加載. inflater.inflate(R.layout.leftfragment, container, true)方法中的第一個參數R.layout.leftfragment是這個Fragment對應的布局文件ID, 第二個參數container是要插入的目標Activity, 第三個參數是決定這個Fragment是否依附於這個container.
LeftFragment對應的布局文件:
1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="match_parent"
4 android:layout_height="match_parent"
5 android:background="@android:color/holo_orange_dark"
6 android:orientation="vertical" >
7
8 <Button
9 android:id="@+id/previous_button"
10 android:layout_width="fill_parent"
11 android:layout_height="wrap_content"
12 android:text="@string/previous_button" />
13
14 <Button
15 android:id="@+id/next_button"
16 android:layout_width="fill_parent"
17 android:layout_height="wrap_content"
18 android:text="@string/next_button" />
19
20 <Button
21 android:id="@+id/exit_button"
22 android:layout_width="fill_parent"
23 android:layout_height="wrap_content"
24 android:text="@string/exit_button" />
25
26 </LinearLayout>
RightFragment類:和LeftFragment類似
1 public class RightFragment extends Fragment
2 {
3 @Override
4 public void onCreate(Bundle savedInstanceState)
5 {
6 super.onCreate(savedInstanceState);
7 System.out.println("RightFragment onCreate");
8 }
9
10 @Override
11 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
12 {
13 System.out.println("RightFragment onCreateView");
14 return inflater.inflate(R.layout.rightfragment, container, true);
15 }
16
17 @Override
18 public void onResume()
19 {
20 super.onResume();
21 System.out.println("RightFragment onResume");
22 }
23
24 @Override
25 public void onPause()
26 {
27 super.onPause();
28 System.out.println("RightFragment onPause");
29 }
30
31 @Override
32 public void onStop()
33 {
34 super.onStop();
35 System.out.println("RightFragment onStop");
36 }
37 }
RightFragment對應的布局文件:
1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="match_parent"
4 android:layout_height="match_parent"
5 android:orientation="vertical" >
6
7 <TextView
8 android:id="@+id/show_message"
9 android:layout_width="fill_parent"
10 android:layout_height="fill_parent"
11 android:background="@android:color/holo_blue_dark"
12 android:text="@string/show_message" />
13
14 </LinearLayout>
最后是Activity的布局文件:
1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="fill_parent"
4 android:layout_height="fill_parent"
5 android:orientation="horizontal" >
6
7 <fragment
8 android:id="@+id/left_fragment"
9 android:name="com.sunflower.LeftFragment"
10 android:layout_width="match_parent"
11 android:layout_height="fill_parent"
12 android:layout_weight="3" />
13
14 <fragment
15 android:id="@+id/right_fragment"
16 android:name="com.sunflower.RightFragment"
17 android:layout_width="match_parent"
18 android:layout_height="fill_parent"
19 android:layout_weight="1" />
20
21 </LinearLayout>
在Activity中的布局文件中加入Fragment標簽,其中android:name屬性對應的就是自定義Fragment類的全名,系統會根據這個調用指定的Fragment的onCreateView()方法來得到這個Fragment的布局,然后加入Activity中. onCreateView()方法中的Container參數就是這時候傳遞過去的。
看看顯示結果:
打開程序時生命周期顯示:
按返回鍵時生命周期顯示: