Fragments是Android 3.0 (API level 11)才引入的.但是它卻又是向下相容的.可以支援老的Android版本.
只不過需要匯入jar包支援(在這個目錄下:android-sdk-windows\extras\android\support\v4\android-support-v4.jar),
主要用於實現以下這種UI布局
想要實現這樣一個activity裡面有多個複雜的View布局, 按照以前的慣用寫法可以使用viewgroup 或者自訂一些布局來實現
而Fragments就恰恰滿足了我們這一需求,他相比我們原來實現的方法,功能更強大,
簡單說來,我個人覺得可以把Fragments看成是一個view,他比view強大的地方是他是有生命週期的 並且這個生命週期會隨著他附著的那個activity的改變而改變.
onCreate()
The system calls this when creating the fragment. Within your implementation, you should initialize essential components of the fragment that you want to retain when the fragment is paused or stopped, then resumed.
onCreateView()
The system calls this when it's time for the fragment to draw its user interface for the first time. To draw a UI for your fragment, you must return a View
from this method that is the root of your fragment's layout. You can return null if the fragment does not provide a UI.
onPause()
The system calls this method as the first indication that the user is leaving the fragment (though it does not always mean the fragment is being destroyed). This is usually where you should commit any changes that should be persisted beyond the current user session (because the user might not come back).
這是他的三個主要的方法,或者說生命週期.當他的父activity onPause();的時候,他也會調用onPause();同樣他的父activityonResume();他調用onResume();
這樣就很靈活,比如有大量bitmap的view,就可以在 onPause();釋放掉無用的資源,再在onResume()的時候載入.
這些Fragments是由FragmentManager這樣一個類似棧的容器管理的,可以通過 findFragmentById()
或者findFragmentByTag()
找到添加到頁面裡面的Fragments.
下面看個小小的DEMO:
首先我們要寫一個繼承Fragment的類,你可以把他看成你的UI介面裡的某一個模組
public class ExampleFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
System.out.println(" onCreateView ");
// Inflate the layout for this fragment
return inflater.inflate(R.layout.example_fragment, container, false);
}
@Override
public void onCreate(Bundle savedInstanceState) {
System.out.println(" onCreate ");
super.onCreate(savedInstanceState);
}
@Override
public void onPause() {
System.out.println(" onPause ");
super.onPause();
}
@Override
public void onResume() {
// TODO Auto-generated method stub
super.onResume();
System.out.println(" onResume ");
}
}
根據列印輸出,你會發現只有第一次啟動才會調用onCreateView;而這裡面就是你這個UI模組的布局
接著是你要展示的activity:
public class MyFragmentActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// ExampleFragment exampleFragment = new ExampleFragment();
// FragmentManager fragmentManager = getSupportFragmentManager();
// FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
// fragmentTransaction.add(R.id.viewer, exampleFragment);
// fragmentTransaction.commit();
}
}
然後activity的布局也非常簡單
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<fragment
android:id="@+id/list"
android:name="com.fragment.demo.ExampleFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<fragment
android:id="@+id/viewer"
android:name="com.fragment.demo.ExampleFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2" />
</LinearLayout>
這樣就已經搞定,產生了2個fragment, 這算是靜態添加吧
如果是動態添加那就需要用到上面注釋的代碼:
ExampleFragment exampleFragment = new ExampleFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.viewer, exampleFragment);
fragmentTransaction.commit();
差不多初識就是這樣,希望有人能補充,下個項目有機會一定要嘗試用用