利用Fragment創建動態UI 之 創建一個Fragment

我們如果把activity界面分割爲多個模塊,那麼一個fragment就可以認爲是其中的一個模塊,這個模塊有着自己的生命週期,可以接收自己的輸入事件,而且在activity運行的時候,你可以動態的增加或者移除它(就好像一個"子activity”,你可以在不同的activity裏面來使用它)。這一節我們就來學習如何利用Support Library來擴展Fragment 類,讓你的app可以在跑着android 1.6老版本的android機器上保持兼容。

注意:如果因爲某些原因,你將你的APP支持的最小的API版本爲11或者之上,那麼你不需要使用Support Library 裏面的,可以直接使用frameworkd層的Fragment 類和對於的android.jar包提供的API函數。要知道,我們在這一節裏面只是學習如何使用Support Library裏面的API函數,這些函數使用了特別的包名,API的名字也和平臺裏面所包含的名稱有些略微不同。

創建一個Fragment類

創建一個由Fragment擴展的fragment,你要去重寫它裏面的生命週期函數,在裏面實現你的APP的邏輯,就像重寫Activity裏面的生命週期方法一樣。

另外一個不同之處是,當你創一個Fragment的時候,你必須使用onCreateView()這個回調函數在定義layout。實際上,這個函數也是如果僅僅是爲了讓你的fragment運行,唯一需要的回調函數。下面就是一個簡單的例子,定義它的layout.

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也需要去實現它的其他的生命週期函數,當它被移除或者添加到一個activity以及activity在不同狀態轉換的時候,這些函數管理着你的fragment的狀態。例如,如果activity的onPause函數被調用,那麼它裏面的fragment也會收到系統對它的onPause的調用。

要查看更多的關於fragment生命週期和回調函數的信息,請閱讀:Fragments

使用XML爲一個Activity添加一個Fragment

fragment是一個可重複利用,獨立的UI模塊組件,每一個Fragment類的實例都必須和FragmentActivity關聯。你可以在activity的layout的xml文件裏面,添加fragment來實現這種關聯。

注意:FragmentActivity 是Support Library 裏面針對系統API 版本在11之下的時候,來處理fragment的類。如果你的APP支持的最小API版本在11或者之上,那就可以直接用Activity來處理fragment。

下面的這個例子就是在large屏幕下的layout xml文件裏面添加了2個fragment:

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>
提示:關於更多的創建不同屏幕下的layout文件,請閱讀Supporting Different Screen ,或者我們前面已經翻譯的章節。

下面是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);
    }
}
注意:如果我們是用xml的方式給一個activity添加一個fragment,那麼,我們無法在activity運行的時候動態的移除這個fragment。如果你想在用戶使用期間讓你的fragment動態在展示或者移除之間亂換,那麼你必須在activity第一次開始的添加fragment到activity。我們在下一節中很快就講解。

 

 

 

 

 

 

 

 

 

 

 

 

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