【Android UI設計與開發】4.底部功能表列(一)Fragment介紹和簡單實現

來源:互聯網
上載者:User

標籤:

TabActivity在Android4.0以後已經被完全棄用,取而代之的是Fragment。Fragment是Android3.0新增的概念,Fragment翻譯成中文是片段的意思,不過卻和Activity十分的相似。以下內容適用於3.0及以上的版本,3.0以下就不再贅述。

官方文檔地址:http://developer.android.com/reference/android/support/v4/app/Fragment.html

一、Fragment的基礎知識介紹

1.Fragment的特性

  Android是在Android 3.0 (API level 11)開始引入Fragment的。

  可以把Fragment想成Activity中的模組,這個模組有自己的布局,有自己的生命週期,單獨處理自己的輸入,在Activity啟動並執行時候可以載入或者移除Fragment模組。

  可以把Fragment設計成可以在多個Activity中複用的模組。

  當開發的應用程式同時適用於平板電腦和手機時,可以利用Fragment實現靈活的布局,改善使用者體驗。

2 .Fragment的生命週期

  因為Fragment必須嵌入在Acitivity中使用,所以Fragment的生命週期和它所在的Activity是密切相關的。

  如果Activity是暫停狀態,其中所有的Fragment都是暫停狀態;如果Activity是stopped狀態,這個Activity中所有的Fragment都不能被啟動;如果Activity被銷毀,那麼它其中的所有Fragment都會被銷毀。

  但是,當Activity在活動狀態,可以獨立控制Fragment的狀態,比如加上或者移除Fragment。

  當這樣進行fragment transaction(轉換)的時候,可以把fragment放入Activity的back stack中,這樣使用者就可以進行返回操作。

3.Fragment的使用相關

  使用Fragment時,需要繼承Fragment或者Fragment的子類(DialogFragment, ListFragment, PreferenceFragment, WebViewFragment),所以Fragment的代碼看起來和Activity的類似。

(1)使用Support Library

  Support Library是一個提供了API庫函數的JAR檔案,這樣就可以在舊版本的Android上使用一些新版本的APIs。

      比如android-support-v4.jar,它的完整路徑是:<sdk>/extras/android/support/v4/android-support-v4.jar.

  

  它就提供了Fragment的APIs,使得在Android 1.6 (API level 4)以上的系統都可以使用Fragment。

  為了確定沒有在舊版本系統上使用新版本的APIs,需要如下匯入語句:

import android.support.v4.app.Fragment;import android.support.v4.app.FragmentManager;

  同時應該將上述的包拷入libs項目下的libs檔案夾,然後在項目的Properties中添加:按右鍵項目,選Properties,左邊選Java Build Path,然後Add External JARs…,添加android-support-v4.jar.

  

  當建立包含Fragment的Activity時,如果用的是Support Library,那麼繼承的就應該是FragmentActivity而不是Activity。

 

(2)一般必須實現的三個回呼函數

  onCreate()

  系統在建立Fragment的時候調用這個方法,這裡應該初始化相關的組件,一些即便是被暫停或者被停止時依然需要保留的東西。

  onCreateView()

  當第一次繪製Fragment的UI時系統調用這個方法,必須返回一個View,如果Fragment不提供UI也可以返回null。

  注意,如果繼承自ListFragment,onCreateView()預設的實現會返回一個ListView,所以不用自己實現。

  onPause()

  當使用者離開Fragment時第一個調用這個方法,需要提交一些變化,因為使用者很可能不再返回來。

 

(3)實現Fragment的UI

  提供Fragment的UI,必須實現onCreateView()方法。

  假設Fragment的布局設定寫在frag_list.xml資源檔中,那麼onCreateView()方法可以如下寫:

public class FragementList extends Fragment{        /**     * 顯示指定的視圖     * @inflater     resource ID,指明了當前的Fragment對應的資源檔     * @container    該Fragment在Activity中的父容器控制項     * @savedInstanceState 是否串連該布局和其父容器控制項,在這裡的情況設定為false,因為系統已經插入了這個布局到父控制項,設定為true將會產生多餘的一個View Group。     */    @Override    public View onCreateView(LayoutInflater inflater, ViewGroup container,            Bundle savedInstanceState) {        return inflater.inflate(R.layout.frag_list, container, false);    }}

(4)把Fragment加入Activity

  當Fragment被加入Activity中時,它會處在對應的View Group中。

  Fragment有兩種載入方式:一種是在Activity的layout中使用標籤<fragment>聲明;另一種方法是在代碼中把它加入到一個指定的ViewGroup中。

  另外,Fragment它可以並不是Activity布局中的任何一部分,它可以是一個不可見的部分。

 

  載入方式1:通過Activity的布局檔案將Fragment加入Activity

  在Activity的布局檔案中,將Fragment作為一個子標籤加入即可。  

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:baselineAligned="false"    android:orientation="horizontal" >    <fragment        android:id="@+id/frag_list"        android:name="com.yanis.ui.FragementList"        android:layout_width="wrap_content"        android:layout_height="match_parent"        android:layout_weight="1" />    <fragment        android:id="@+id/frag_detail"        android:name="com.yanis.ui.FragementDetails"        android:layout_width="wrap_content"        android:layout_height="match_parent"        android:layout_weight="2" /></LinearLayout>

  其中android:name屬性填上建立的fragment的完整類名。

  當系統建立這個Activity的布局檔案時,系統會執行個體化每一個fragment,並且調用它們的onCreateView()方法,來獲得相應fragment的布局,並將傳回值插入fragment標籤所在的地方。

  有三種方法為Fragment提供ID:

  android:id屬性:唯一的id

  android:tag屬性:唯一的字串

  如果上面兩個都沒提供,系統使用容器view的ID。

    

  載入方式2:通過編程的方式將Fragment加入到一個ViewGroup中

  當Activity處於Running狀態下的時候,可以在Activity的布局中動態地加入Fragment,只需要指定加入這個Fragment的父View Group即可。

  首先,需要一個FragmentTransaction執行個體:

FragmentManager fragmentManager = getFragmentManager()FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

 (注,如果import android.support.v4.app.FragmentManager;即用的是Support Library,那麼使用的是:FragmentManager fragmentManager = getSupportFragmentManager();)

 之後,用add()方法加上Fragment的對象:  

ExampleFragment fragment = new ExampleFragment();fragmentTransaction.add(R.id.fragment_container, fragment);fragmentTransaction.commit();

  其中第一個參數是這個fragment的容器,即父控制群組。

  最後需要調用commit()方法使得FragmentTransaction執行個體的改變生效。

 

4.來個簡單栗子吧

 

方式一:通過Activity的布局檔案將Fragment加入Activity

  如下:

  

  布局檔案如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:baselineAligned="false"    android:orientation="horizontal" >    <fragment        android:id="@+id/frag_list"        android:name="com.yanis.ui.FragementList"        android:layout_width="wrap_content"        android:layout_height="match_parent"        android:layout_weight="1" />    <fragment        android:id="@+id/frag_detail"        android:name="com.yanis.ui.FragementDetails"        android:layout_width="wrap_content"        android:layout_height="match_parent"        android:layout_weight="2" /></LinearLayout>

 

方式二:通過編程的方式將Fragment加入到一個ViewGroup中

  如下:

  

  1.首頁面布局檔案如下

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:baselineAligned="false"    android:orientation="horizontal" >    <LinearLayout        android:id="@+id/frag_list"        android:layout_width="wrap_content"        android:layout_height="match_parent"        android:layout_weight="1" android:orientation="vertical">        <TextView            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:text="List" />    </LinearLayout>    <LinearLayout        android:id="@+id/frag_detail" android:orientation="vertical"        android:layout_width="wrap_content"        android:layout_height="match_parent"        android:layout_weight="2" >        <TextView            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:text="Details" />    </LinearLayout></LinearLayout>

  2.Activity類代碼如下:

package com.yanis.ui;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    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        //setContentView(R.layout.activity_main);        setContentView(R.layout.activity_maino);        FragmentManager fragmentManager = getSupportFragmentManager();        FragmentTransaction fragmentTransaction = fragmentManager                .beginTransaction();        FragementList fragment1 = new FragementList();        fragmentTransaction.add(R.id.frag_list, fragment1);        FragementDetails fragment2 = new FragementDetails();        fragmentTransaction.add(R.id.frag_detail, fragment2);        fragmentTransaction.commit();    }}

 

原始碼地址:https://github.com/YeXiaoChao/Yc_ui_fragment

【Android UI設計與開發】4.底部功能表列(一)Fragment介紹和簡單實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.