一、Fragment概述
1.Fragment可以作爲Activity界面的一部分組成出現;
2.可以在一個Activity中同時出現多個Fragment,並且一個Fragment也可以在多個Activity中使用;
3.在Activity運行過程中,可以添加、移除或替換Fragment;
4.Fragment可以響應自己的輸入事件,並且有自己的聲明週期,它們的生命週期會受宿主Activity的生命週期影響。
二、如何創建一個Fragment?
(1)必須創建一個Fragment的子類,或繼承自已經存在的某個Fragment子類;
導入的包是import android.app.Fragment; 而不是 //import android.support.v4.app.Fragment; 後者在Debug時有問題。
(2)重寫onCreateView()方法中加載屬於Fragment自己的佈局文件。Fragment第一次繪製它的用戶界面的時候,系統會調用此方法,爲了繪製Fragment的UI,此方法必須返回一個View,如果不顯示,返回null即可。
package com.example.fragmenttest;
import android.app.Fragment;
//import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class MyFragment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//layout佈局文件轉View對象
/**
* 參數1:Fragment需要加載的佈局文件
* 參數2:加載layout的父ViewGroup,要想把fragment添加到Activity中,
* 需要把View對象放入ViewGroup中來,所以這裏需要給它傳遞一個ViewGroup對象
* 參數3:false——不返回父ViewGroup
*/
View view= inflater.inflate(R.layout.fragment, container, false);
TextView text=(TextView) view.findViewById(R.id.text);
text.setText("靜態加載Fragment");
return view;
}
}
//在Activity的類中,可以直接訪問當前Activity所包含的Fragment的下的組件的ID
三、Fragment的加載方式
1.靜態加載
在Activity的layout文件中聲明Fragment,需要特別注意的是<fragment>中的android:name屬性指定了在layout中實例化的Fragment類。
標識Fragment的方法:android:id屬性提供一個唯一ID(必須加ID,否則報錯)、 android:tag屬性提供一個唯一字符串。
Activity與Fragment的邏輯關係:
Activity的onCreate()中調用setContentView(R.layout.main2); //main2.xml中含有fragment標籤,其android:name屬性中指定了Fragment派生類對象名,在Fragment派生類的onCreateView()中inflater.inflate(R.layout.fragment, container, false)中指定了對應的fragment.xml。
2.動態加載
(1)撰寫代碼將Fragment添加到一個Activity layout中
add():添加一個Fragment(指定要添加的fragment和要插入的View),與次類似的還有remove()、替換()
(2)要動態加載fragment需處理Fragment事務:根據用戶的交互情況,對Fragment進行添加、移除、替換、以及執行其他動作,提交給Activity的每一套變化被稱爲一個事務。
獲取Fragment事務的方法:
FragmentManager fragmentManager=getFragmentManager();
FragmentTransaction beginTransaction = fragmentManager.beginTransaction(); //開啓一個事務,在事務裏去添加、移除fragment等動作
每一個事務都是同時執行一套變化,可以再一個事務中設置你所有想執行的變化,包括add()、remove()、replace(),然後提交給Activity,必須調用commit()方法。
如果允許用戶通過按下BACK按鍵返回到前一個Fragment狀態,調用commit()之前可以加入addToBackStack(null)方法。
動態添加
fragment: beginTransaction.add(R.id.frame,fragment2); //R.id.frame對應要加入的Activity的XML文件的某個標籤的id,如<LinearLayout android:id="@+id/frame" > </LinearLayout> ,fragment2是創建的Fragment子類對象,如靜態加載一樣,在onCreateView()方法中會指定fragment2對應的Layout XML文件。
實例代碼
MyFragment2 fragment2=new MyFragment2();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction beginTransaction = fragmentManager.beginTransaction();
beginTransaction.add(R.id.frame, fragment2);
beginTransaction.addToBackStack(null);
beginTransaction.commit();
四、Fragment的生命週期
1.void onAttach(Activity activity) //當Fragment被添加到Activity的時候會回調這個方法,並且只調用一次;
2.void onCreate(Bundle savedInstanceState) //創建Fragment時會回調,只會調用一次;
3.View onCreateView(***) //每次創建都會繪製Fragment的View組件時回調該方法
4.void onActivityCreated(Bundle savedInstanceState) //Fragment所在的Activity啓動完成後調用
5.void onStart() //啓動Fragment
6.void onResume() //回覆Fragment時會回調,調用onStart()方法後面一定會調用onResume()方法。
7.void onPause() //暫停Fragment
8.void onStop() //停止Fragment
9. void onDestroyView() //銷燬Fragment所包含的View組件時調用,與onCreateView(***)對應
10.void Destroy() //銷燬Fragment時會被回調
11.void onDetach() //與onAttach()對應,Fragment從Activity中刪除時會回調該函數,並且這個方法只會調用一次