Fragment的介紹
Fragment可以當成Activity的一個界面的一個組成部分,甚至Activity的界面可以完全有不同的Fragment組成赛惩,更帥氣的是Fragment擁有自己的生命周期和接收蟹演、處理用戶的事件谒主,這樣就不必在Activity寫一堆控件的事件處理的代碼了,并且占用內(nèi)存降低,性能大幅度提高筐高。更為重要的是占婉,你可以動態(tài)的添加泡嘴、替換和移除某個Fragment。
Fragment的生命周期
- onAttach()當該Fragment被添加到Activity時被回調(diào)逆济。該方法只會被調(diào)用一次酌予。
- onCreate()創(chuàng)建Fragment時被回調(diào)。該方法只會被回調(diào)一次
- onCreateView()每次創(chuàng)建奖慌、繪制該Fragment的View組件時回調(diào)該方法抛虫,F(xiàn)ragment會顯示該方法返回的View對象
- onActivityCreated() 當Fragment所在的Activity被啟動完成后回調(diào)該方法。當Activity的onCreate()方法返回之后調(diào)用該方法简僧。
- onStart()啟動Fragment時被回調(diào)
- onResume()恢復Fragment時回調(diào)該方法建椰,在onStart()方法后一定會回調(diào)該方法。
- onPause() 暫停Fragment時回調(diào)該方法
- onStop() 停止Fragment時被回調(diào)
- onDestroyView() 銷毀該Fragment所包含的View組件時調(diào)用
- onDestroy() 銷毀Fragment時被回調(diào)
- onDetach() 將該Fragment從Activity中刪除岛马、替換完成時回調(diào)該方法棉姐,在onDestroy()方法后一定會回調(diào)onDetach()方法。該方法只會被回調(diào)一次啦逆。
Fragment的使用
1.靜態(tài)使用
首先創(chuàng)建兩個XML文件,fragment1.xml和fragment2.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00ff00" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="碎片1"
android:textColor="#000000"
android:textSize="25sp" />
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffff00" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="碎片2"
android:textColor="#000000"
android:textSize="25sp" />
</LinearLayout>
然后創(chuàng)建兩個兩個類,Fragment1和Fragment2分別繼承Fragment,注意伞矩,使用的V4包中的Fragment!
public class Fragment1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment1, container, false);
}
}
public class Fragment2 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment2, container, false);
}
}
最后在activity_main.xml里面使用兩個fragment
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/fragment1"
android:name="yin.layout.Fragment1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<fragment
android:id="@+id/fragment2"
android:name="yin.layout.Fragment2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
2.動態(tài)使用
首先在activity_main.xml里面的布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/btn_show_fragment1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="顯示Fragment1"/>
<Button
android:id="@+id/btn_show_fragment2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="顯示Fragment2"/>
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
然后還是創(chuàng)建2個fragment,可以使用快速創(chuàng)建
最后是在MainActivity里使用代碼進行動態(tài)添加
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnLoadFrag1 = (Button)findViewById(R.id.btn_show_fragment1);
btnLoadFrag1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
Fragment1 fragment1 = new Fragment1();
transaction.add(R.id.fragment_container, fragment1);
transaction.commit();
}
});
Button btnLoagFrag2 = (Button)findViewById(R.id.btn_show_fragment2);
btnLoagFrag2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
Fragment2 fragment2 = new Fragment2();
transaction.add(R.id.fragment_container, fragment2);
transaction.commit();
}
});
}
}
其實動態(tài)添加最主要的就是這幾個步奏(注意! transaction不要復用!!!)
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
Fragment1 fragment1 = new Fragment1();
transaction.add(R.id.fragment_container, fragment); //添加
transaction.show(R.id.fragment_container, fragment); //顯示
transaction.hide(R.id.fragment_container, fragment); //隱藏
transaction.replace(R.id.fragment_container, fragment); //替換
transaction.remove(R.id.fragment_container, fragment); //移除
transaction.commit();
Fragment的傳參
敬請期待