【Android Training - 03】使用Fragments建立動態的UI [ Lesson 2 - 新建一個Fragment ]


Creating a Fragment


你可以把fragment當作activity的一部分,它有自己的lifecycle,它會接受自己的輸入事件。你可以在activity運行的時候增加或者拿掉fragment。(類似子activity,你可以在不同的activity中重用fragment)。這節課演示如何使用support library來創建一個繼承自 Fragment 的類。

Note: 如果你的app決定跑在Level 11或者更高的系統上,你不需要添加這個support library,你可以使用那個版本的系統自帶的API來實現。請注意這節課是專注於使用support library裏面的API,這裏面的API名稱與那些本身就包含這些功能的平臺上的名稱有可能不一樣。

Create a Fragment Class [創建一個fragment類]

爲了新建一個fragment,需要繼承 Fragment 類,然後重寫關鍵的lifecycle中的callback方法,類似Activity的寫法。

創建fragment,其中一個不同的地方是你必須使用 onCreateView() 的回調來定義Layout。實際上,這是使得fragment開始運行的唯一的callback。如下:

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.ViewGroup;

public class ArticleFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
        Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.article_view, container, false);
    }
}

就想一個activity一樣,一個fragment應該實現一些其他的callback,這樣你可以操控當它添加到activity,從activity移除,或者activity在不同生命週期進行切換時fragment該做的動作。l例如,當activity的 onPause() 方法被呼叫時,所有在這個activity中的fragment也都會呼叫到 onPause().方法。

更多關於fragment的lifecycle與callback的方法,請參考 Fragments 開發者指南。

Add a Fragment to an Activity using XML [使用XML添加fragment到activity]

因爲fragment可以重用,是模塊化的組件,每一個fragment的實例都與父類FragmentActivity有依賴,你可以通過定義每一個fragment的XML來實現這樣的聯繫。

Note: FragmentActivity 是一個在Support Library中的特殊的activity,使用它來向Level 11以下的系統進行兼容。如果你是在Level 11以上開發,則可以使用通常的 Activity.

這裏有一個layout的示例文件,當設備屏幕被認爲是large級別時,它會添加了兩個fragment到一個activity中。

res/layout-large/news_articles.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <fragment android:name="com.example.android.fragments.HeadlinesFragment"
              android:id="@+id/headlines_fragment"
              android:layout_weight="1"
              android:layout_width="0dp"
              android:layout_height="match_parent" />

    <fragment android:name="com.example.android.fragments.ArticleFragment"
              android:id="@+id/article_fragment"
              android:layout_weight="2"
              android:layout_width="0dp"
              android:layout_height="match_parent" />

</LinearLayout>
下面則演示了一個activity如何運用這個layout:

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

public class MainActivity extends FragmentActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.news_articles);
    }
}
Note: 當你通過定義一個layout XML的方式來添加fragment到activity時,你不可以在運行時移除這個fragment.如果你計劃在與用戶進行交互時對fragment進行載入與移除,你必須在activity第一次啓動時進行添加fragment,下一節課會講到。


學習自:http://developer.android.com/training/basics/fragments/creating.html

轉載請註明出自:http://blog.csdn.net/kesenhoo,謝謝!



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章