1. 程式人生 > >Fragment小結

Fragment小結

star hello mov 查看 bundle 返回 手機 fcm 程序

Fragment是Android3.0之後增加的新特性,通常人們叫它碎片。可是,我認為把它理解成一個View模塊比較好,盡管它不是繼承自View。假設閱讀過源代碼就知道它是內置View對象從而實現View的特性。在設計模式裏面通常說到,擴展一個類的方式有2種,第一就是類繼承,第二就是對象組合,而開發經驗告訴我們。要多用對象組合。所以Fragment是直接繼承Object,組合View來實現View的特性的。

類繼承:

這裏先看一下Fragment的類繼承結構:

技術分享技術分享

生命周期:

Fragment與Activity一樣具有生命周期,例如以下:

技術分享

技術分享

而Fragment的生命周期是附著在使用Fragment的Activity的生命周期之上。這裏有個圖片來比較下:

技術分享

技術分享

源代碼分析:

Fragment.java位於SDK的android.app包下,通過查看源代碼,能夠看到Fragment的實現方式是組合View對象和ViewGroup對象。在Fragment還有startActivity和startActivityFroResult方法,是不是跟Activity非常像?!在使用Fragment的時候一般要實現2個生命周期方法onCreate和onPause,方便保存數據,在重寫onCreateView時,能夠向ViewGroup對象container中加入某個布局文件代表的View。形如return inflater.inflate(R.layout.yourlayout,container, false);


Fragment的基本運用

分為布局文件裏使用Fragment和在程序中動態加入Fragment

布局文件裏使用Fragment

第一步:為Fragment指定一個布局文件。例如以下fragmentlayout.xml

<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" >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
</RelativeLayout>

第二步:自己寫一個類MyFragment繼承Fragment類

package com.example.myfragmentlayout;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class MyFragment extends Fragment{
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
		return inflater.inflate(R.layout.fragmentlayout, container, false);
	}
}

重寫onCreateView方法。返回自定義的Fragment的layout。這裏註意onCreateView方法的參數ViewGroup對象!

特別註意。

第三步:在主Activity的布局文件activity_main.xml中這樣寫道

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >
    <fragment
        android:name="com.example.myfragmentlayout.MyFragment"
        android:tag="dxd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
</RelativeLayout>

這裏務必為fragment指定一個tag或是id屬性。!

不然執行會出錯。


在程序中動態加入Fragment

還記的重寫Fragment中的onCreateView方法中的第二個參數吧,是ViewGroup對象。說明Fragment是被加入到ViewGroup對象中去的。從SDK文檔中能夠看到ViewGroup的子類是非常多的,這裏截個圖

技術分享

技術分享

有了這個基礎。我們在程序中動態加入Fragment.

前一二步都跟在布局文件裏加入Fragment一樣的,僅僅有在主Activity的layout文件裏指定Fragment的方式不一樣,這裏我們指定的是一個ViewGroup子類。

第3步

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >
    <FrameLayout
        android:id="@+id/framelayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</RelativeLayout>

這裏定義的是FrameLayout對象,它是ViewGroup的子類,它作為一個容器把Fragment加入進去。

那麽在程序中應該如何去加入呢?

package com.example.myfragmentlayout;

import android.app.Activity;
import android.app.FragmentTransaction;
import android.os.Bundle;

public class MainActivity extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		MyFragment fragment = new MyFragment();
		FragmentTransaction ft = getFragmentManager().beginTransaction() ;
		ft.add(R.id.framelayout, fragment);
		ft.commit() ;
	}
}

從代碼中能夠看到,是將fragment對象增加到FrameLayout中去。

最後別忘了commit提交。這裏必須註意了。對於同一個FragmentTransaction對象僅僅能提交一次!!

使用FragmentTransaction時須要多註意一下。

由此我們能夠想象到。我們能夠依據不同的需求選擇適當的容器來裝Fragment,如ScrollView,LinearLayout等

假設你通過繼承LinearLayout來自己定義過控件,那麽你肯定對Fragment有非常似成相識的感覺。當你繼承LinearLayout時須要實現3個構造方法。在這3個構造方法中,你通過LayoutInflater將layout布局文件轉換成View,然後為這個View賦予各種監聽方法和事件響應。

反過來思考一下Fragment是不是這樣實現的呢?當我們繼承Fragment時,我們須要實現onCreateView方法,該方法中就是通過LayoutInflater對象將layout布局文件轉換成View。然後為這個View加入各種響應時間和時間響應,最後加入到ViewGroup的子類中去。

Fragment的事務處理

Fragment的事物處理是通過FragmentTransaction來實現的,它是通過FragmentManager得到的。這裏說一下它幾個經常使用的方法:

add :向某個ViewGroup容器中加入Fragment對象

hide:隱藏某個Fragment。讓其不可見。

remove:移除某個Fragment

show:顯示某個Fragment

setCustomAnimations:指定Fragment切換時的動畫

commit:提交,同一個FragmentTransaction僅僅能commit一次,不然會出錯。

Fragment的綜合應用:

模仿手機QQ主界面的4個視圖。詳情請見某大神的總結:http://blog.csdn.net/guolin_blog/article/details/13171191

Fragment小結