1,Fragment的作用
Fragment作爲Activity的一部分有自己的生命週期方法,但是會受Activity的影響。主要作用可以用來做屏幕適配,一套代碼可以用於平板電視已經手機上,只需更改不同的佈局即可。Fragment可以自己接受處理用戶的點擊觸摸事件,你可以動態的添加移除替換Fragment,來達到你的要求。主要用來一個Activity中有多個模塊的時候講不通的模塊交給不通的Fragment來處理,避免在Activity中寫一大堆代碼,有利於項目後期的維護。
2,Fragment的生命週期
上面既然說了Fragment有自己的生命週期,那麼下面通過兩張圖片來看一下Fragment的生命週期是怎樣的。
Fragment的生命週期方法
Fragment的生命週期方法與Activity的生命週期的關係,Activity的生命週期會直接影響Fragment的生命週期
相對Activity來說,這裏我們來談一談幾個Activity中沒有的方法
onAttach(),當fragment與Activity產生關聯時調用
onCreateView(),用來產生view的方法,返回的view對象即Fragment的視圖,一般使用View.inflate()方法將xml轉化爲view
onActivityCreated(), 當Activity的oncreate()方法返回時調用
onDestoryView(),與onCreateView對應,當該Fragment的視圖被移除時調用
onDetach()與onAttach對應,當Fragment與Activity關聯被取消時調用
注意:除了onCreateView,其他的所有方法如果你重寫了,必須調用父類對於該方法的實現
聲明週期回調的具體時機:
場景演示 : 切換到該Fragment
11-29 14:26:35.095: D/AppListFragment(7649): onAttach
11-29 14:26:35.095: D/AppListFragment(7649): onCreate
11-29 14:26:35.095: D/AppListFragment(7649): onCreateView
11-29 14:26:35.100: D/AppListFragment(7649): onActivityCreated
11-29 14:26:35.120: D/AppListFragment(7649): onStart
11-29 14:26:35.120: D/AppListFragment(7649): onResume
屏幕滅掉:
11-29 14:27:35.185: D/AppListFragment(7649): onPause
11-29 14:27:35.205: D/AppListFragment(7649): onSaveInstanceState
11-29 14:27:35.205: D/AppListFragment(7649): onStop
屏幕解鎖
11-29 14:33:13.240: D/AppListFragment(7649): onStart
11-29 14:33:13.275: D/AppListFragment(7649): onResume
切換到其他Fragment:
11-29 14:33:33.655: D/AppListFragment(7649): onPause
11-29 14:33:33.655: D/AppListFragment(7649): onStop
11-29 14:33:33.660: D/AppListFragment(7649): onDestroyView
切換回本身的Fragment:
11-29 14:33:55.820: D/AppListFragment(7649): onCreateView
11-29 14:33:55.825: D/AppListFragment(7649): onActivityCreated
11-29 14:33:55.825: D/AppListFragment(7649): onStart
11-29 14:33:55.825: D/AppListFragment(7649): onResume
回到桌面
11-29 14:34:26.590: D/AppListFragment(7649): onPause
11-29 14:34:26.880: D/AppListFragment(7649): onSaveInstanceState
11-29 14:34:26.880: D/AppListFragment(7649): onStop
回到應用
11-29 14:36:51.940: D/AppListFragment(7649): onStart
11-29 14:36:51.940: D/AppListFragment(7649): onResume
退出應用
11-29 14:37:03.020: D/AppListFragment(7649): onPause
11-29 14:37:03.155: D/AppListFragment(7649): onStop
11-29 14:37:03.155: D/AppListFragment(7649): onDestroyView
11-29 14:37:03.165: D/AppListFragment(7649): onDestroy
11-29 14:37:03.165: D/AppListFragment(7649): onDetach
3,Fragmend的使用
- 靜態使用
靜態使用Fragment是比較簡單的,相當與把Fragment當做一個控件來使用
使用步驟:
- 1 繼承Fragment,重寫onCreateView()來構建Fragment的視圖
ContentFragment佈局文件中
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:gravity="center"
android:layout_height="match_parent">
<TextView
android:gravity="center"
android:textSize="25sp"
android:text="Fragment當作主面板"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
TitleFragment的佈局文件中
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="@android:color/darker_gray"
android:layout_height="45dp">
<ImageButton
android:id="@+id/im_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@android:drawable/star_on"
/>
<TextView
android:textSize="25sp"
android:textColor="@android:color/holo_red_dark"
android:gravity="center"
android:text="Fragment製作標題欄"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
2 在Activity的佈局文件中聲明
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
>
<fragment
android:id="@+id/fg_title"
android:layout_width="match_parent"
android:layout_height="45dp"
android:name="app.linfeng.com.myapplication.TitleFragment"
/>
<fragment
android:layout_below="@id/fg_title"
android:id="@+id/fg_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="app.linfeng.com.myapplication.ContentFragment"
/>
</RelativeLayout>
3.在各自的Fragment中處理點擊事件
- 動態的使用
在項目中一般常用的還是動態的使用Fragment來添加移除替換視圖
1.Activity的佈局文件
<RelativeLayout 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" >
<fragment
android:id="@+id/id_fragment_title"
android:name="com.zhy.zhy_fragments.TitleFragment"
android:layout_width="fill_parent"
android:layout_height="45dp" />
<include
android:id="@+id/id_ly_bottombar"
android:layout_width="fill_parent"
android:layout_height="55dp"
android:layout_alignParentBottom="true"
layout="@layout/bottombar" />
<FrameLayout
android:id="@+id/id_content"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@id/id_ly_bottombar"
android:layout_below="@id/id_fragment_title" />
</RelativeLayout>
Activity中的代碼
package com.zhy.zhy_fragments;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.LinearLayout;
public class MainActivity extends Activity implements OnClickListener
{
private LinearLayout mTabWeixin;
private LinearLayout mTabFriend;
private ContentFragment mWeixin;
private FriendFragment mFriend;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
// 初始化控件和聲明事件
mTabWeixin = (LinearLayout) findViewById(R.id.tab_bottom_weixin);
mTabFriend = (LinearLayout) findViewById(R.id.tab_bottom_friend);
mTabWeixin.setOnClickListener(this);
mTabFriend.setOnClickListener(this);
// 設置默認的Fragment
setDefaultFragment();
}
private void setDefaultFragment()
{
FragmentManager fm = getFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
mWeixin = new ContentFragment();
transaction.replace(R.id.id_content, mWeixin);
transaction.commit();
}
@Override
public void onClick(View v)
{
FragmentManager fm = getFragmentManager();
// 開啓Fragment事務
FragmentTransaction transaction = fm.beginTransaction();
switch (v.getId())
{
case R.id.tab_bottom_weixin:
if (mWeixin == null)
{
mWeixin = new ContentFragment();
}
// 使用當前Fragment的佈局替代id_content的控件
transaction.replace(R.id.id_content, mWeixin);
break;
case R.id.tab_bottom_friend:
if (mFriend == null)
{
mFriend = new FriendFragment();
}
transaction.replace(R.id.id_content, mFriend);
break;
}
// transaction.addToBackStack();
// 事務提交
transaction.commit();
}
}
使用的是替換的方式實現的,replace(),相當於把Fragment的onCreateView返回的view添加到FrameLayout這一容器中顯示
效果圖如下:
Fragment是3.0之後出現的,在3.0以下的版本中需要引入v4包,通過 getSupportFragmentManager獲得FragmentManager。不過現在3.0以下的系統佔比很小,近乎不用考慮這個問題,而且最新的v7包中getSupportFragmentManager就可以兼容低版本
使用Fragment常見的API
Fragment 用來定義Fragment
FragmentManager 用來在Activity中管理和操作Fragment
FragmentTransaction 事務
a,獲取FragmentManeger 在Activity中getFragmentManager,向下兼容getSupportFragmentManager;
b,獲取事務,主要操作時靠事務來完成的
FragmentTransaction transaction = fm.benginTransatcion();
transaction.add();向Activity中加入一個Fragment
transaction.remove();移除一個Fragment,如果一個Fragmetn沒有加入回退棧,這個Fragment的實例會被銷燬
transaction.replace();使用一個新的Frament替換掉另外一個Fragment,實際就是先remove後add;
transaction.hide(); 隱藏當前的Fragment,只是不可見,實例並不會銷燬
transaction.show();顯示之前隱藏的Fragment
transaction.detach();將Fragment中的view衝UI中移除
transaction.attach();重新創建view視圖,並顯示在UI
transaction.commit();提交事務
commit方法一定要在Activity.onSaveInstance()之前調用
常見的事例
a、比如:我在FragmentA中的EditText填了一些數據,當切換到FragmentB時,如果希望會到A還能看到數 據,則適合你的就是hide和show;也就是說,希望保留用戶操作的面板,你可以使用hide和show,當然了不要使勁在那new實例,進行下非 null判斷。
b、再比如:我不希望保留用戶操作,你可以使用remove(),然後add();或者使用replace()這個和remove,add是相同的效果。
c、 remove和detach有一點細微的區別,在不考慮回退棧的情況下,remove會銷燬整個Fragment實例,而detach則只是銷燬其視圖結 構,實例並不會被銷燬。那麼二者怎麼取捨使用呢?如果你的當前Activity一直存在,那麼在不希望保留用戶操作的時候,你可以優先使用detach。