標籤:android style blog http io color ar os 使用
Fragment要點
- Fragment作為Activity介面的一部分組成出現
- 可以在一個Activity中同時出現多個Fragment,並且,一個Fragment亦可在多個Activity中使用。
- 在Activity運行過程中,可以添加、移除或者替換Fragment(add()、remove()、replace())
- Fragment可以響應自己的輸入事件,並且有自己的生命週期,當然,它們的生命週期直接被其所屬的宿主activity的生命週期影響。
設計哲學
Android在3.0中引入了fragments的概念,主要目的是用在大螢幕裝置上--例如平板電腦上,支援更加動態和靈活的UI設計。平板電腦的螢幕要比手機的大得多,有更多的空間來放更多的UI組件,並且這些組件之間會產生更多的互動。Fragment允許這樣的一種設計,而不需要你親自來管理 viewhierarchy的複雜變化。 通過將activity的布局分散到fragment中, 你可以在運行時修改activity的外觀,並在由activity管理的back stack中儲存那些變化.(http://developer.android.com/guide/topics/fundamentals/fragments.html)
例如, 一個新聞應用可以在螢幕左側使用一個fragment來展示一個文章的列表,然後在螢幕右側使用另一個fragment來展示一篇文章--2個fragment並排顯示在相同的一個activity中,並且每一個fragment擁有它自己的一套生命週期回調方法,並且處理它們自己的使用者輸入事件。 因此, 取代使用一個activity來選擇一篇文章而另一個activity來閱讀文章的方式,使用者可以在同一個activity中選擇一篇文章並且閱讀, :
fragment在你的應用中應當是一個模組化和可重用的組件.即,因為fragment定義了它自己的布局, 以及通過使用它自己的生命週期回調方法定義了它自己的行為,你可以將fragment包含到多個activity中. 這點特別重要, 因為這允許你將你的使用者體驗適配到不同的螢幕尺寸.舉個例子,你可能會僅當在螢幕尺寸足夠大時,在一個activity中包含多個fragment,並且,當不屬於這種情況時,會啟動另一個單獨的,使用不同fragment的activity.
繼續之前那個新聞的例子 -- 當運行在一個特別大的螢幕時(例如平板電腦),應用可以在Activity A中嵌入2個fragment。然而,在一個正常尺寸的螢幕(例如手機)上,沒有足夠的空間同時供2個fragment用, 因此, Activity A會僅包含文章列表的fragment, 而當使用者選擇一篇文章時, 它會啟動ActivityB,它包含閱讀文章的fragment.因此, 應用可以同時支援中的2種設計模式。
建立Fragment 要建立一個fragment, 必須建立一個 Fragment 的子類 (或者繼承自一個已存在的它的子類). Fragment類的代碼看起來很像 Activity 。它包含了和activity類似的回調方法, 例如onCreate()、 onStart()、onPause()以及 onStop()。事實上, 如果你準備將一個現成的Android應用轉換到使用fragment,可能只需簡單的將代碼從你的activity的回調方法分別移動到你的fragment的回調方法即可。
通常, 應當至少實現如下的生命週期方法:
- onCreate()
當建立fragment時, 系統調用該方法.
在實現代碼中,應當初始化想要在fragment中保持的必要組件, 當fragment被暫停或者停止後可以恢複.
- onCreateView()
fragment第一次繪製它的使用者介面的時候, 系統會調用此方法. 為了繪製fragment的UI,此方法必須返回一個View, 這個view是你的fragment布局的根view. 如果fragment不提供UI, 可以返回null.
- onPause()
使用者將要離開fragment時,系統調用這個方法作為第一個指示(然而它不總是意味著fragment將被銷毀.) 在目前使用者會話結束之前,通常應當在這裡提交任何應該持久化的變化(因為使用者有可能不會返回).
其生命週期圖如下:
大多數應用應當為每一個fragment實現至少這3個方法,但是還有一些其他回調方法你也應當用來去處理fragment生命週期的各種階段.全部的生命週期回調方法將會在後面章節 Handlingthe Fragment Lifecycle 中討論.
除了繼承基類 Fragment , 還有一些子類你可能會繼承:
- DialogFragment
顯示一個浮動的對話方塊.
用這個類來建立一個對話方塊,是使用在Activity類的對話方塊工具方法之外的一個好的選擇,
因為你可以將一個fragment對話方塊合并到activity管理的fragment back stack中,允許使用者返回到一個之前曾被摒棄的fragment.
- ListFragment
顯示一個由一個adapter(例如 SimpleCursorAdapter)管理的項目的列表, 類似於ListActivity.
它提供一些方法來管理一個list view, 例如 onListItemClick()回調來處理點擊事件.
- PreferenceFragment
顯示一個 Preference對象的階層的列表, 類似於PreferenceActivity.
這在為你的應用建立一個"設定"activity時有用處.
好了,下來都是官方的列子我就不多說了,直接上我的fragment建立選項卡詳細列子:
import com.menglifang.frag.FragmentOne;import com.menglifang.frag.FragmentTwo;import com.menglifang.frag.Fragmentfour;import com.menglifang.frag.Fragmentfree;import android.os.Bundle;import android.support.v4.app.FragmentActivity;import android.support.v4.app.FragmentManager;import android.support.v4.app.FragmentTransaction;import android.view.View;import android.view.View.OnClickListener;import android.widget.FrameLayout;import android.widget.LinearLayout;import android.widget.TextView;public class MainActivity extends FragmentActivity implements OnClickListener{ FrameLayout frameLayout; TextView text1,text2,text3,text4; FragmentManager fm; private FragmentTransaction ft; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); frameLayout=(FrameLayout ) findViewById(R.id.frag_view); text1=(TextView) findViewById(R.id.tab_one); text2=(TextView) findViewById(R.id.tab_two); text3=(TextView) findViewById(R.id.tab_free); text4=(TextView) findViewById(R.id.tab_four); text1.setOnClickListener(this); text2.setOnClickListener(this); text3.setOnClickListener(this); text4.setOnClickListener(this); fm=this.getSupportFragmentManager(); ft=fm.beginTransaction(); ft.replace(R.id.frag_view, new FragmentOne()); ft.commit(); } @Override public void onClick(View arg0) { ft=fm.beginTransaction(); switch(arg0.getId()){ case R.id.tab_one: ft.replace(R.id.frag_view, new FragmentOne()); break; case R.id.tab_two: ft.replace(R.id.frag_view, new FragmentTwo()); break; case R.id.tab_free: ft.replace(R.id.frag_view, new Fragmentfree()); break; case R.id.tab_four: ft.replace(R.id.frag_view, new Fragmentfour()); break; } ft.commit(); } }
建立了四個Fragement:為了簡單我的四個fragment中每個Fragement否是一樣的就是各自的布局檔案不同
import com.menglifang.fragment.R;import android.os.Bundle;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;public class FragmentOne extends Fragment{ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub View view=inflater.inflate(R.layout.fragone, null); return view;//super.onCreateView(inflater, container, savedInstanceState); }}
布局檔案:在此只給出MainActivity中的布局和一個fragment中的布局
MainActivity中的布局: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" tools:context=".MainActivity" ><LinearLayout android:id="@+id/tab" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:id="@+id/tab_one" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="最新新聞"/> <TextView android:id="@+id/tab_two" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="熱點新聞"/> <TextView android:id="@+id/tab_free" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="社會新聞"/> <TextView android:id="@+id/tab_four" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="利物浦新聞"/></LinearLayout> <FrameLayout android:orientation="vertical" android:id="@+id/frag_view" android:layout_below="@+id/tab" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ffff00"></FrameLayout></RelativeLayout>
一個fragment的布局:fragone.xml
<?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="200dp" android:orientation="vertical" > <TextView android:background="#ff0000" android:gravity="center" android:layout_width="match_parent" android:layout_height="match_parent" android:text="這是第一個frage"/></LinearLayout>
其實這個列子好簡單,一看基本就明白了,我在網上查了,好多人都是拿官網的列子複製下來,我覺得實實在在的列子比較容易讓人理解
多有不足歡迎大家學習交流
android中用Fragment的簡單介紹和建立選項卡詳細列子