Android Fragment基礎篇
最近用到Fragment,又重新整理了一下,同時也準備再次開啟部落格之旅,希望能堅持去下。
一、引言
Fragment是Android 3.0(API level 11)以後提出的系統元件,主要目的是在大螢幕裝置上支援更加動態和靈活的UI設計,從而改善使用者的體驗。
Fragment中文意思為碎片or片段,簡單地可以理解為是Activity(活動)的一部分,在它的上面可以顯示各種控制元件,但是Fragment又不同於普通的View(檢視),它直屬於Object類,本人認為這是谷歌攻城師們拋開現有介面框架的束縛,設計的一個全新的視覺元件。
Fragment作為
二、生命週期
我們應該都已知道Activity有佈局,有生命週期,而Fragment作為Activity的一部分,它也有自己的佈局和生命週期,可以單獨處理自己的輸入,在Activity執行的時候可以動態地載入或者移除Fragment模組。官方文件中提供的生命週期圖如下:
該流程圖雖然涉及的方法眾多,但是總得來講還是比較簡單,如果對Activity生命週期有很好的理解的話。在這裡就不對每一個方法做闡述了,意思也是很顯然的,大多數應用程式至少應該實現下面三個方法
1.onCreate()
系統在建立Fragment的時候呼叫這個方法,這裡應該初始化相關的元件,一些即便是被暫停或者被停止時依然需要保留的東西。
2.onCreateView()
用於建立Fragment的顯示檢視,當第一次繪製Fragment的UI時系統呼叫這個方法,返回值型別是一個View,如果Fragment不提供介面也可以返回null。
3.onPause()
當用戶離開Fragment時第一個呼叫這個方法,需要提交一些變化,因為使用者很可能一去不復回。
當然還有其它幾個回撥方法可應該按情況實現之。
三、派生類
1.DialogFragment
一個浮動在Activity
2.ListFragment
顯示一個列表控制元件,就像 ListActivity 類,它提供了很多管理列表的方法。
3.PreferenceFragment
是一個具有SharedPreferences功能的Fragment。
四、Fragment的使用
步驟一:新增支援庫(可選)
如果您的專案支援3.0以下的版本,需要有支援庫。支援庫是一個提供了API庫函式的JAR檔案,這樣就可以在舊版本的Android上使用一些新版本的API。
比如常用的android-support-v4.jar.它的完整路徑是:
<sdk>/extras/android/support/v4/android-support-v4.jar.
它就提供了Fragment的API,使得在Android 1.6 (API level 4)以上的系統都可以使用Fragment。具體匯入方法在此不再贅述,可以參考(http://www.cnblogs.com/kissazi2/p/3644848.html)。
步驟二:建立一個Fragment的佈局檔案此佈局檔案和Activity的佈局檔案一樣,根據需要設計好介面,假設檔名為flagment_layout.xml,比如:
<span style="font-size:14px;"><span style="font-size:12px;"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
android:background="#112233"
>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
android:textColor="#ff0000"
android:layout_marginTop="50dp"
android:text="這是我的第一個Fragment" />
</LinearLayout></span>
</span>
步驟三:自定義Fragment類
此類需要繼承Fragment類,同時重寫相應的方法。假設檔案儲存為MyFlagment.java,程式碼如下:
<span style="font-size:12px;">package com.example.fragment;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class MyFragment extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.flagment_layout, container, false);
return view;
}
@Override
public void onPause() {
super.onPause();
}
}</span>
步驟四:新增Fragment並顯示
這裡有2種方法顯示前面定義好的Fragment,一種是通過Activity佈局中使用<fragment>標籤,另一種是在程式碼中把Fragment物件新增到指定的ViewGroup中,具體實現如下。
1.XML佈局方式
<span style="font-size:12px;"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="下面是Fragment" />
<fragment
<span style="color:#FF0000;">android:name="com.example.fragment.MyFragment"</span>
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
</span>
其中android:name屬性填上自定義fragment的完整類名。當系統建立這個Activity的佈局檔案時,系統會例項化每一個fragment,並且呼叫它們的onCreateView()方法,來獲得相應fragment的佈局,並將返回值插入fragment標籤所在的地方。
2.程式碼方式
在這裡可以在Activity的佈局檔案中定義一個容器佈局(ViewGroup),例如是一個FrameLayout,指定一個ID,在程式碼中用於存放Fragment。
<span style="font-size:12px;"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="下面是Fragment" />
<span style="color:#FF0000;"><FrameLayout
android:id="@+id/frameLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/></span>
</LinearLayout></span>
Activity定義如下:<span style="font-size:12px;">package com.example.fragment;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
public class MainActivity extends FragmentActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fragmentManager = getSupportFragmentManager();//如果不用支援庫則getFragmentManager獲取物件
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
MyFragment fragment = new MyFragment();
fragmentTransaction.add(<span style="color:#FF0000;">R.id.frameLayout</span>, fragment);
fragmentTransaction.commit();
}
}</span>
這裡要注意的是,如果要支援3.0以下版本,相關的類都必須是支援庫所在包中的,其中Activity要繼承自FragmentActivity(如果是3.0以上版本直接可以繼承Activity)。FragmentManager能夠實現管理Activity中fragment,通過呼叫activity的getFragmentManager() 或 getSupportFragmentManager()取得它的例項。 FragmentTransaction表示事務,能對fragment進行新增、移除、替換、以及執行其他動作。
本次實驗最終效果如下: