Fragment是activity的界面中的一部分或一種行爲。能夠把多個Fragment組合到一個activity中來建立一個多界面而且能夠在多個activity中重用一個Fragment。能夠把Fragment任務模塊化的一段activity,它具備本身的生命週期,接收它本身的事件,並能夠在activity運行時被添加或刪除。html
本文地址:http://www.cnblogs.com/wuyudong/p/5893804.html,轉載請註明源地址。java
Fragment不能獨立存在,它必須嵌入到activity中,並且Fragment的生命週期直接受所在的activity的影響。例如:當activity暫停時,他擁有的全部的Fragment都暫停了,當activity銷燬時,他擁有的全部Fragment都被銷燬。然而,當activity運行時(在onResume()以後,onPause()以前),能夠單獨地操做每一個Fragment,好比添加或刪除它們。當中執行上述針對Fragment的事務時,能夠將事務添加到一個棧中,這個棧被activity管理,棧中的每一條都是一個Fragment的一次事務。有了這個棧,就能夠反向執行Fragment的事務,這樣就能夠在Fragment級支持「返回」鍵(向後導航)。android
當向activity中添加一個Fragment時,它須置於ViewGroup控件中,而且需定義Fragment本身的界面。能夠在layout.xml佈局文件中聲明Fragment,元素爲:<fragment>;也能夠在代碼中建立Fragment,而後把它加入到ViewGroup控件中。然而,Fragment不必定非要放在activity的界面中,它能夠隱藏在後臺爲activity工做。ide
實戰一下模塊化
項目佈局文件代碼:佈局
<LinearLayout 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:orientation="horizontal" tools:context=".MainActivity" > <fragment android:id="@+id/fragment1" android:name="com.wuyudong.fragment.Fragment1" android:layout_width="0dip" android:layout_height="fill_parent" android:layout_weight="1" > </fragment> <fragment android:id="@+id/fragment2" android:name="com.wuyudong.fragment.Fragment2" android:layout_width="0dip" android:layout_height="fill_parent" android:layout_weight="1" > </fragment> </LinearLayout>
接着在layout文件夾下新建兩個fragment.xml文件spa
fragment1.xml:code
<?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="match_parent" android:background="#0000ff" android:orientation="vertical" > </LinearLayout>
fragment2.xmlxml
<?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="match_parent" android:background="#ff00" android:orientation="vertical" > </LinearLayout>
接着在項目源代碼文件夾下新建兩個java文件htm
Fragment1.java
public class Fragment1 extends Fragment { /** * 當fragment被建立的時候,調用的方法,返回當前fragment顯示的內容 */ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment1, null); } }
Fragment2.java
public class Fragment2 extends Fragment { /** * 當fragment被建立的時候,調用的方法,返回當前fragment顯示的內容 */ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment2, null); } }
運行項目
接下來實現動態建立Fragment
在剛纔的項目的基礎上,新建一個項目。刪除原來activity_main.xml代碼中的fragment1與fragment2代碼段,其餘的代碼不變
接下來在MainActivity中添加下面的代碼:
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 一、判斷當前手機的朝向 int width = getWindowManager().getDefaultDisplay().getWidth(); int height = getWindowManager().getDefaultDisplay().getHeight(); Fragment1 fragment1 = new Fragment1(); Fragment2 fragment2 = new Fragment2(); FragmentManager fm = getFragmentManager(); FragmentTransaction ft = fm.beginTransaction(); if (width > height) { // 水平方向 ft.replace(android.R.id.content, fragment1); } else { ft.replace(android.R.id.content, fragment2); } ft.commit(); } }
上面的代碼實現了當手機不一樣朝向的時候,顯示的不一樣的fragment