1. 程式人生 > >Android之Fragment的靜態使用

Android之Fragment的靜態使用

1 Fragment的產生與介紹

Fragment,也就是碎片,本意是為了適配大螢幕的安卓裝置而生的。執行Android的裝置繁多,螢幕大小更是多種多樣。針對不同螢幕尺寸,通常情況下,開發者都是先針對手機開發一套原始碼,然後拷貝一份,修改佈局以適應大螢幕裝置,或平板等。為了決解這樣的麻煩,Google推出了Fragment。你可以把Fragment當成Activity的一個介面的一個組成部分,甚至Activity的介面可以完全有不同的Fragment組成,Fragment擁有自己的生命週期和接收、處理使用者的事件,這樣就不必在Activity寫一堆控制元件的事件處理的程式碼了。更為重要的是,你可以動態的新增、替換和移除某個Fragment。
2 Fragment的生命週期:
這裡寫圖片描述


可以看到Fragment比Activity多了幾個額外的生命週期回撥方法:
onAttach(Activity)
當Fragment與Activity發生關聯時呼叫。
onCreateView(LayoutInflater, ViewGroup,Bundle)
建立該Fragment的檢視
onActivityCreated(Bundle)
當Activity的onCreate方法返回時呼叫
onDestoryView()
與onCreateView想對應,當該Fragment的檢視被移除時呼叫
onDetach()
與onAttach相對應,當Fragment與Activity關聯被取消時呼叫
注意:除了onCreateView,其他的所有方法如果你重寫了,必須呼叫父類對於該方法的實現。
3 靜態使用Fragment:
1、繼承Fragment,重寫onCreateView決定Fragemnt的佈局,新建兩個FragmentOne,FragmentTwo類
2.新增XML佈局控制元件:

public class FragmentOne extends Fragment {
    private TextView mTextView;
    // 相當於Activity的的onCreate
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        // 讓碎片載入一個佈局 引數 :1 佈局 2 container  3固定 false
        View view = inflater.inflate(R.layout.fragment_one,container,false
); mTextView = (TextView) view.findViewById(R.id.text); mTextView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(getContext(), "上下文,要用getContext()來實現", Toast.LENGTH_SHORT).show(); } }); return view; } }

FragmentOne .XML中的佈局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <TextView
        android:background="@drawable/pre13"
        android:id="@+id/text"
        android:text="我是一個標題"
        android:textSize="50sp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>

FragmentTwo中的類:

public class FragmentTwo extends Fragment {

    private TextView mTextView;

    // 相當於Activity的的onCreate
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        // 讓碎片載入一個佈局 引數 :1 佈局 2 container  3固定 false
        View view = inflater.inflate(R.layout.fragment_two,container,false);

        mTextView = (TextView) view.findViewById(R.id.text2);
        mTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getContext(), "我還是我", Toast.LENGTH_SHORT).show();
            }
        });
        return view;
    }
}

FragmentTwo 的.XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:background="#00f0f0"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <TextView
        android:gravity="center"
        android:id="@+id/text2"
        android:text="我是中央主題"
        android:textSize="50sp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>

2.MainActivity中的.XML(因為是靜態Fragment,MainActivity中可以什麼都不用些,只需要載入控制元件就可):

<?xml version="1.0" encoding="utf-8"?>
<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="com.zhiyuan3g.fragment_static.MainActivity">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <fragment
            android:id="@+id/fragment_title"
            android:name="com.zhiyuan3g.fragment_static.FragmentOne"
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="0dp"/>

        <fragment
            android:id="@+id/fragment_text"
            android:layout_weight="9"
            android:name="com.zhiyuan3g.fragment_static.FragmentTwo"
            android:layout_width="match_parent"
            android:layout_height="0dp"/>
    </LinearLayout>

</RelativeLayout>

最後我們的效果圖就出來啦:
這裡寫圖片描述

靜態的使用,更多的是把Fragment當成普通的View一樣宣告在Activity的佈局檔案中,然後所有控制元件的事件處理等程式碼都由各自的Fragment去處理。