一、什么是Fragment
Fragment是Activity界面中的一部分,可以理解為模塊化的Activity
1.Fragment不能獨(dú)立存在,必須嵌入到Activity中。
2.Fragment具有自己的生命周期,接收自己的事件,并可以在Activity運(yùn)行時(shí)被添加或刪除。
3.Fragment的生命周期直接受所在的Activity的影響。
二、Fragment的作用
支持靈活、動(dòng)態(tài)的界面設(shè)計(jì)
1.Fragment從Android3.0后引入
2.在低版本Android 3.0前使用Fragment,需要采用android-support-v4.jar兼容包
三、Fragment生命周期解析
-
首先看官方說(shuō)明圖
Fragment各個(gè)方法的調(diào)用場(chǎng)景
- onAttach方法
Fragment與Activity建立關(guān)聯(lián)的時(shí)候調(diào)用 - onCreateView方法
為Fragment創(chuàng)建視圖(加載布局)時(shí)調(diào)用(給當(dāng)前的fragment繪制UI布局,可以使用線程更新UI) - onActivityCreated方法
當(dāng)Activity中的onCreate方法執(zhí)行完后調(diào)用(表示activity執(zhí)行onCreate方法完成了的時(shí)候會(huì)調(diào)用此方法) - onDestroyView方法
Fragment中的布局被移除時(shí)調(diào)用(表示fragment銷毀相關(guān)聯(lián)的UI布局) - onDetach方法
Fragment和Activity解除關(guān)聯(lián)的時(shí)候調(diào)用(脫離Activity)
Fragment生命周期解析
從創(chuàng)建到顯示
- 當(dāng)一個(gè)fragment被創(chuàng)建的時(shí)候:
onAttach()
onCreate()
onCreateView()
onActivityCreated() - 當(dāng)這個(gè)fragment對(duì)用戶可見(jiàn)的時(shí)候,它會(huì)經(jīng)歷以下?tīng)顟B(tài):
onStart()
onResume()
后臺(tái)模式 - 當(dāng)這個(gè)fragment進(jìn)入“后臺(tái)模式”的時(shí)候,它會(huì)經(jīng)歷以下?tīng)顟B(tài):
onPause()
onStop()
銷毀 - 當(dāng)這個(gè)fragment被銷毀了(或者持有它的activity被銷毀了):
onPause()
onStop()
onDestoryView()
onDetroy()
onDetach()
Bundle保存 - 就像Activity一樣,在以下的狀態(tài)中,可以使用Bundle對(duì)象保存一個(gè)fragment的對(duì)象
onCreate()
onCreateView()
onActivityCreated()
其它場(chǎng)景的調(diào)用
- 屏幕滅掉:onPause(),onSaveInstanceState(),onStop()
- 屏幕解鎖:onStart(),onResume()
- 切換到其它fragment:onPause(),onStop(),onDestroyView()
- 切換回本身fragment:onCreateView(),onActivity()Created(),onStart(),onResume()
- 回到桌面:onPause(),onSaveInstanceState()
- 回到應(yīng)用:onStart(),onResume()
- 退出應(yīng)用:onPause(),onStop(),onDestroyView(),onDestroy(),onDetach()
Fragment的具體使用方法
方法一:在Activity的layout.xml布局文件中靜態(tài)添加
-
Activity的布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > // 該fragment類定義在包名為"com.skywang.app"中的FragmentLayoutTest類的內(nèi)部類ExampleFragment中 <fragment android:name="com.skywang.app.FragmentLayoutTest$ExampleFragment" android:id="@+id/list" android:layout_weight="1" android:layout_width="match_parent" android:layout_height="match_parent"/> </LinearLayout> -
Fragment布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" ><TextView android:text="@string/example_fragment" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout> -
Activity文件
// 在Activity使用Fragment時(shí),需要考慮版本兼容問(wèn)題 // 1. Android 3.0后,Activity可直接繼承自Activity,并且在其中嵌入使用Fragment // 2. Android 3.0前,Activity需FragmentActivity(其也繼承自Activity),同時(shí)需要導(dǎo)入android-support-v4.jar兼容包,這樣在Activity中才能嵌入Fragment public class FragmentLayoutTest extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fragment_layout_test); // 設(shè)置上述布局文件 } // 繼承自Fragment // 布局文件中的Fragment通過(guò)該FragmentLayoutTest的內(nèi)部類ExampleFragment實(shí)現(xiàn) public static class ExampleFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.example_fragment, container, false); // 將example_fragment.xml作為該Fragment的布局文件 // 即相當(dāng)于FragmentLayoutTest直接調(diào)用example_fragment.xml來(lái)顯示到Fragment中 } }}
方法二:在Activity文件中動(dòng)態(tài)添加
-
步驟1:在Activity的布局文件定義1占位符(FrameLayout),這樣做的好處是:可動(dòng)態(tài)在Activity中添加不同的Fragment,更加靈活。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <FrameLayout android:id="@+id/about_fragment_container" android:layout_width="match_parent" android:layout_height="match_parent"/> </LinearLayout> -
步驟2:設(shè)置Fragment布局文件
<LinearLayout xmlns:android="http:// schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/ tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:text="@string/example_fragment" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout> -
步驟3:在Activity文件動(dòng)態(tài)添加Fragment
public class FragmentTransactionTest extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fragment_transaction_test); // 步驟1:獲取FragmentManager FragmentManager fragmentManager = getFragmentManager(); // 步驟2:獲取FragmentTransaction FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); // 步驟3:創(chuàng)建需要添加的Fragment :ExampleFragment ExampleFragment fragment = new ExampleFragment(); // 步驟4:動(dòng)態(tài)添加fragment // 即將創(chuàng)建的fragment添加到Activity布局文件中定義的占位符中(FrameLayout) fragmentTransaction.add(R.id.about_fragment_container, fragment); fragmentTransaction.commit(); } // 繼承與Fragment public static class ExampleFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.example_fragment, container, false); // 將example_fragment.xml作為該Fragment的布局文件 } } }
