fragment
android是在android 3.0 (api level 11)開(kāi)始引入fragment的。
可以把fragment想成activity中的模塊,這個(gè)模塊有自己的布局,有自己的生命周期,單獨(dú)處理自己的輸入,在activity運(yùn)行的時(shí)候可以加載或者移除fragment模塊。
可以把fragment設(shè)計(jì)成可以在多個(gè)activity中復(fù)用的模塊。
當(dāng)開(kāi)發(fā)的應(yīng)用程序同時(shí)適用于平板電腦和手機(jī)時(shí),可以利用fragment實(shí)現(xiàn)靈活的布局,改善用戶體驗(yàn)。
如圖:

fragment的生命周期
因?yàn)閒ragment必須嵌入在acitivity中使用,所以fragment的生命周期和它所在的activity是密切相關(guān)的。
如果activity是暫停狀態(tài),其中所有的fragment都是暫停狀態(tài);如果activity是stopped狀態(tài),這個(gè)activity中所有的fragment都不能被啟動(dòng);如果activity被銷毀,那么它其中的所有fragment都會(huì)被銷毀。
但是,當(dāng)activity在活動(dòng)狀態(tài),可以獨(dú)立控制fragment的狀態(tài),比如加上或者移除fragment。
當(dāng)這樣進(jìn)行fragment transaction(轉(zhuǎn)換)的時(shí)候,可以把fragment放入activity的back stack中,這樣用戶就可以進(jìn)行返回操作。

fragment的使用相關(guān)
使用fragment時(shí),需要繼承fragment或者fragment的子類(dialogfragment, listfragment, preferencefragment, webviewfragment),所以fragment的代碼看起來(lái)和activity的類似。
使用support library
support library是一個(gè)提供了api庫(kù)函數(shù)的jar文件,這樣就可以在舊版本的android上使用一些新版本的apis。
比如android-support-v4.jar.它的完整路徑是:
<sdk>/extras/android/support/v4/android-support-v4.jar.
它就提供了fragment的apis,使得在android 1.6 (api level 4)以上的系統(tǒng)都可以使用fragment。
為了確定沒(méi)有在舊版本系統(tǒng)上使用新版本的apis,需要如下導(dǎo)入語(yǔ)句:
復(fù)制代碼 代碼如下:
import android.support.v4.app.fragment;
import android.support.v4.app.fragmentmanager;
同時(shí)應(yīng)該將上述的包拷入libs項(xiàng)目下的libs文件夾,然后在項(xiàng)目的properties中添加:右鍵單擊項(xiàng)目,選properties,左邊選java build path,然后add external jars…,添加android-support-v4.jar.

當(dāng)創(chuàng)建包含fragment的activity時(shí),如果用的是support library,那么繼承的就應(yīng)該是fragmentactivity而不是activity。
必須實(shí)現(xiàn)的三個(gè)回調(diào)函數(shù)
oncreate()
系統(tǒng)在創(chuàng)建fragment的時(shí)候調(diào)用這個(gè)方法,這里應(yīng)該初始化相關(guān)的組件,一些即便是被暫停或者被停止時(shí)依然需要保留的東西。
oncreateview()
當(dāng)?shù)谝淮卫L制fragment的ui時(shí)系統(tǒng)調(diào)用這個(gè)方法,必須返回一個(gè)view,如果fragment不提供ui也可以返回null。
注意,如果繼承自listfragment,oncreateview()默認(rèn)的實(shí)現(xiàn)會(huì)返回一個(gè)listview,所以不用自己實(shí)現(xiàn)。
onpause()
當(dāng)用戶離開(kāi)fragment時(shí)第一個(gè)調(diào)用這個(gè)方法,需要提交一些變化,因?yàn)橛脩艉芸赡懿辉俜祷貋?lái)。
實(shí)現(xiàn)fragment的ui
提供fragment的ui,必須實(shí)現(xiàn)oncreateview()方法。
假設(shè)fragment的布局設(shè)置寫在example_fragment.xml資源文件中,那么oncreateview()方法可以如下寫:
復(fù)制代碼 代碼如下:
public static class examplefragment extends fragment
{
@override
public view oncreateview(layoutinflater inflater, viewgroup container,
bundle savedinstancestate)
{
// inflate the layout for this fragment
return inflater.inflate(r.layout.example_fragment, container, false);
}
}
oncreateview()中container參數(shù)代表該fragment在activity中的父控件;savedinstancestate提供了上一個(gè)實(shí)例的數(shù)據(jù)。
inflate()方法的三個(gè)參數(shù):
第一個(gè)是resource id,指明了當(dāng)前的fragment對(duì)應(yīng)的資源文件;
第二個(gè)參數(shù)是父容器控件;
第三個(gè)布爾值參數(shù)表明是否連接該布局和其父容器控件,在這里的情況設(shè)置為false,因?yàn)橄到y(tǒng)已經(jīng)插入了這個(gè)布局到父控件,設(shè)置為true將會(huì)產(chǎn)生多余的一個(gè)view group。
把fragment加入activity
當(dāng)fragment被加入activity中時(shí),它會(huì)處在對(duì)應(yīng)的view group中。
fragment有兩種加載方式:一種是在activity的layout中使用標(biāo)簽<fragment>聲明;另一種方法是在代碼中把它加入到一個(gè)指定的viewgroup中。
另外,fragment它可以并不是activity布局中的任何一部分,它可以是一個(gè)不可見(jiàn)的部分。這部分內(nèi)容先略過(guò)。
加載方式1:通過(guò)activity的布局文件將fragment加入activity
在activity的布局文件中,將fragment作為一個(gè)子標(biāo)簽加入即可。
如:
復(fù)制代碼 代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment android:name="com.example.news.articlelistfragment"
android:id="@+id/list"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
<fragment android:name="com.example.news.articlereaderfragment"
android:id="@+id/viewer"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="match_parent" />
</linearlayout>
其中android:name屬性填上你自己創(chuàng)建的fragment的完整類名。
當(dāng)系統(tǒng)創(chuàng)建這個(gè)activity的布局文件時(shí),系統(tǒng)會(huì)實(shí)例化每一個(gè)fragment,并且調(diào)用它們的oncreateview()方法,來(lái)獲得相應(yīng)fragment的布局,并將返回值插入fragment標(biāo)簽所在的地方。
有三種方法為fragment提供id:
android:id屬性:唯一的id
android:tag屬性:唯一的字符串
如果上面兩個(gè)都沒(méi)提供,系統(tǒng)使用容器view的id。
加載方式2:通過(guò)編程的方式將fragment加入到一個(gè)viewgroup中
當(dāng)activity處于running狀態(tài)下的時(shí)候,可以在activity的布局中動(dòng)態(tài)地加入fragment,只需要指定加入這個(gè)fragment的父view group即可。
首先,需要一個(gè)fragmenttransaction實(shí)例:
fragmentmanager fragmentmanager = getfragmentmanager()
fragmenttransaction fragmenttransaction = fragmentmanager.begintransaction();(注,如果import android.support.v4.app.fragmentmanager;那么使用的是:fragmentmanager fragmentmanager = getsupportfragmentmanager();)
之后,用add()方法加上fragment的對(duì)象:
examplefragment fragment = new examplefragment();
fragmenttransaction.add(r.id.fragment_container, fragment);
fragmenttransaction.commit();
其中第一個(gè)參數(shù)是這個(gè)fragment的容器,即父控件組。
最后需要調(diào)用commit()方法使得fragmenttransaction實(shí)例的改變生效。
實(shí)例
練習(xí)的例子:
寫一個(gè)類繼承自fragment類,并且寫好其布局文件(本例中是兩個(gè)textview),在fragment類的oncreateview()方法中加入該布局。
之后用兩種方法在activity中加入這個(gè)fragment:
第一種是在activity的布局文件中加入<fragment>標(biāo)簽;
第二種是在activity的代碼中使用fragmenttransaction的add()方法加入fragment。
貼出代碼:
自己定義的fragment類:
復(fù)制代碼 代碼如下:
examplefragment.java
package com.example.learningfragment;
import android.os.bundle;
import android.support.v4.app.fragment;
import android.view.layoutinflater;
import android.view.view;
import android.view.viewgroup;
public class examplefragment extends fragment
{
//三個(gè)一般必須重載的方法
@override
public void oncreate(bundle savedinstancestate)
{
// todo auto-generated method stub
super.oncreate(savedinstancestate);
system.out.println("examplefragment--oncreate");
}
@override
public view oncreateview(layoutinflater inflater, viewgroup container,
bundle savedinstancestate)
{
system.out.println("examplefragment--oncreateview");
return inflater.inflate(r.layout.example_fragment_layout, container, false);
}
@override
public void onpause()
{
// todo auto-generated method stub
super.onpause();
system.out.println("examplefragment--onpause");
}
@override
public void onresume()
{
// todo auto-generated method stub
super.onresume();
system.out.println("examplefragment--onresume");
}
@override
public void onstop()
{
// todo auto-generated method stub
super.onstop();
system.out.println("examplefragment--onstop");
}
}
fragment的布局文件:
復(fù)制代碼 代碼如下:
example_fragment_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<textview
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/num1"
/>
<textview
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/num2"
/>
</linearlayout>
主activity:
復(fù)制代碼 代碼如下:
learnfragment.java
package com.example.learningfragment;
import android.os.bundle;
import android.support.v4.app.fragmentactivity;
import android.support.v4.app.fragmentmanager;
import android.support.v4.app.fragmenttransaction;
public class learnfragment extends fragmentactivity
{
@override
public void oncreate(bundle savedinstancestate)
{
super.oncreate(savedinstancestate);
setcontentview(r.layout.activity_learn_fragment);
//在程序中加入fragment
fragmentmanager fragmentmanager = getsupportfragmentmanager();
fragmenttransaction fragmenttransaction = fragmentmanager.begintransaction();
examplefragment fragment = new examplefragment();
fragmenttransaction.add(r.id.linear, fragment);
fragmenttransaction.commit();
}
}
activity的布局文件:
復(fù)制代碼 代碼如下:
activity_learn_fragment.xml
<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"
>
<button
android:id="@+id/btn1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/btn1"
/>
<fragment
android:name="com.example.learningfragment.examplefragment"
android:id="@+id/fragment1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<button
android:id="@+id/btn2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/btn2"
/>
<linearlayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linear"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<button
android:id="@+id/btn3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/btn3"
/>
</linearlayout>
</linearlayout>
運(yùn)行結(jié)果如下:

可以看到第二種方式加入fragment的時(shí)候,指定了父容器(一個(gè)線性布局)的id,其中已經(jīng)有一個(gè)button 3,所以fragment加在其后。
參考資源
fragment類文檔:
http://developer.android.com/reference/android/app/fragment.html
training:building a dynamic ui with fragments
http://developer.android.com/training/basics/fragments/index.html
fragments develop guide:
http://developer.android.com/guide/components/fragments.html