Android Fragment 基本介紹

2018-08-11 11:46 更新

Fragment

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

可以把Fragment想成Activity中的模塊,這個(gè)模塊有自己的布局,有自己的生命周期,單獨(dú)處理自己的輸入,在Activity運(yùn)行的時(shí)候可以加載或者移除Fragment模塊。

可以把Fragment設(shè)計(jì)成可以在多個(gè)Activity中復(fù)用的模塊。

當(dāng)開發(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。

為了確定沒有在舊版本系統(tǒng)上使用新版本的APIs,需要如下導(dǎo)入語(yǔ)句:

  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)建包含F(xiàn)ragment的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)的組件,一些即便是被暫?;蛘弑煌V箷r(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)用戶離開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()方法可以如下寫:

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中。

另外,F(xiàn)ragment它可以并不是Activity布局中的任何一部分,它可以是一個(gè)不可見的部分。這部分內(nèi)容先略過。

加載方式1:通過Activity的布局文件將Fragment加入Activity

在Activity的布局文件中,將Fragment作為一個(gè)子標(biāo)簽加入即可。

如:

<?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è)都沒提供,系統(tǒng)使用容器view的ID。

加載方式2:通過編程的方式將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類:

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的布局文件:

<?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:

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的布局文件:

<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加在其后。

轉(zhuǎn)載來(lái)自:博客

以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)