一、Fragment的簡單使用java
<1>Fragment:在Activity中使用的碎片,有本身的佈局、生命週期和輸入事件android
<2>使用Fragment的步驟ide
(1)建立類,並繼承Fragment;佈局
public class FirstFragment extends Fragment{ }
(2)重寫Fragment的onCreateView()生命週期方法,並返回一個View;code
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO 加載fragment視圖(兩種方法,推薦使用第二種) //View view = inflater.inflate(R.layout.first_fragment, null); View view2 = inflater.inflate(R.layout.first_fragment, container, false); TextView textView = (TextView) view2.findViewById(R.id.textView); //返回加載的視圖對象 return view2;
(3)使用fragment的兩種方法對象
a.在佈局文件中使用<fragment android:name="自定義Fragment的類路徑"/>保證碎片顯示惟一:id/tag繼承
<fragment android:id="@+id/firstFragment" android:layout_width="match_parent" android:layout_height="150dp" android:layout_below="@id/tv" android:name="com.qf.day12demo01.FirstFragment" />
b.動態添加生命週期
1.獲取到一個fragment管理器對象事件
FragmentManager manager = getFragmentManager();
2.經過這個管理器對象要開啓一個事務事務
FragmentTransaction transaction = manager.beginTransaction();
3.把預留在佈局中的位置,換成要展現的碎片對象
transaction.replace(R.id.replaceId, new SecondFragment());
4.提交事務
transaction.commit();
<3>使用FragmentManager
(1)做用:管理多個Fragment之間的交互和傳值
(2)Activity.getFragmentManger() 3.0之後
(3)FragmentActivity.getSupportFragmentManager() 3.0之前,引用v4包
(4)FragmentTransaction beginTransaction() 獲取Fragment事務處理對象
<4>s使用FragmentTransaction
(1)replace(int containerViewId, Fragment fragment) 把預留在佈局中的位置,換成要展現的碎片對象
(2)commit() 提交本次事務處理
三、Fragment的生命週期
<1>11個生命週期方法
(1)onAttach(Activity) 鏈接宿主Activity
(2)onCreate(Bundle) 建立Fragment
(3)onCreateView(LayoutInflater, ViewGroup, Bundle)建立Fragment視圖
(4)onActivityCreated(Bundle) 當宿主Activity的onCreate()執行完以後調用
(5)onStart()
(6)onResume()
(7)onPause()
(8)onStop()
(9)onDestroyView() 銷燬Fragment視圖,與onCreateView對應
(10)onDestroy() 銷燬Fragment,與onCreate對應
(11)onDetach() 與宿主Activity斷開鏈接,與onAttach對應
<2>生命週期流程
(1)當Activity建立時,調用Fragment的onAttach->onCreate->onCreateView->onActivityCreated
(2)當Activity啓動時,調用Fragment的onStart
(3)當Activity獲取焦點時,調用Fragment的onResume
(4)當跳轉到另外一個Activity時,調用Fragment的onPause-->onStop
(5)當返回Activity時,調用Fragment的onStart->onResume
(6)銷燬Activity時,調用Fragment的onDestroyView->onDestory->onDettach
三、Fragment與Activity之間的傳值
<1>Activity-->Fragment
(1)在activity中添加碎片的時候,經過碎片對象的.setArgments(bundle)
public void btnSendMsg(View view){ TextView tv_top = (TextView) findViewById(R.id.tv_top); tv_top.setText(content+new Date()); FragmentTransaction transaction = manager.beginTransaction(); bottomFragment = new BottomFragment(); Bundle bundle = new Bundle(); bundle.putString("msg", content+new Date()); //把碎片對象和要傳遞的數據綁定 bottomFragment.setArguments(bundle); transaction.replace(R.id.replaceId, bottomFragment);//替換的碎片對象中是綁定有數據的 transaction.commit(); }
(2)在fragment裏面,經過getArgments();獲得一個bundle對象,再從bundle對象裏面獲取內容
Bundle bundle = getArguments(); if (bundle != null) { String msg = bundle.getString("msg"); tv_bottom.setText(msg); }
<2>Fragment-->Activity
(1)在activity中聲明一個公共的方法,在這個方法中必需要有一個參數(參數類型就是要傳遞的數據類型)
public void setContent(String s){ tv_main.setText(s); }
(2)在fragment裏面,經過getActivity(),能夠獲取到宿主activity對象,再調用宿主對象中到提供的公共方法,把數據傳遞在這個方法中
@Override public void onClick(View v) { // TODO 向宿主activity傳值 //獲取到當前碎片所在的宿主activity對象 MainActivity activity = (MainActivity) getActivity(); activity.setContent(content+new Date()); }
(3)Activity方式
1.在Activity中獲取Fragment中的UI控件,並增長相關事件
2.在Activity聲明公共方法,在Fragment中調用getActivity()並強轉,則能夠調用公共方法向其餘Fragment控件傳值
3.獲取assests下的文件流 InputStream is = getResources().getAssets().open("day01.txt");