Android利用Fragment實現Tab佈局
下圖爲最終效果圖:
(可以看出,使用Fragment與ViewPager的實現效果一樣,需注意佈局效果可自定義, 此處只講解實現的邏輯步驟
邏輯步驟:
1、創建兩個Fragment,分別代表btn1和btn2單擊後的佈局。
2、創建兩個Fragment對應的佈局文件(這兩步驟就當做Activity使用即可)
3、主佈局文件中,使用FragmentLayout組件進行頁面的佔位
4、主Activity中,監聽其按鈕單擊,分別調用Fragment在FragmentLayout中顯示即可
1)下圖爲整個項目工程結構
2)定義Fragment類
public class FragmentOne extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_one, container, false);
}
}
返回的View類型代表將會在界面中顯示的視圖文件,這裏引用到了fragment_one.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="match_parent"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="fragment one"
android:textSize="20pt" />
</LinearLayout>
3)定義主界面佈局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@+id/id_fragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<LinearLayout
android:id="@+id/id_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/id_btn1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="btnStartClick"
android:text="btn1" />
<Button
android:id="@+id/id_btn2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="btnStartClick"
android:text="btn2" />
</LinearLayout>
</LinearLayout>
4)MainActivity.java類
private FragmentManager fm;
private FragmentTransaction ft;
private List<Fragment> fragments;
以下介紹FragmentTransaction操作的核心方法:
add(): 向Activity中添加一個Fragment
remove()
從Activity中移除一個Fragment,如果被移除的Fragment沒有添加到回退棧(回退棧後面會詳細說),這個Fragment實例將會被銷燬。
replace()
使用另一個Fragment替換當前的,實際上就是remove()然後add()的合體~
hide()
隱藏當前的Fragment,僅僅是設爲不可見,並不會銷燬
show()
顯示之前隱藏的Fragment
detach()
會將view從UI中移除,和remove()不同,此時fragment的狀態依然由FragmentManager維護。
attach()
重建view視圖,附加到UI上並顯示。
commit()//提交一個事務
<span style="white-space:pre"> </span>/**
* 初始化
*/
private void init() {
if (fragments == null) {
fragments = new ArrayList<Fragment>();
}
fragments.add(new FragmentOne());
fragments.add(new FragmentTwo());
// 以上步驟都爲初始化一個List<Fragment> 數據
fm = getFragmentManager(); // 在Activity中可直接獲取到Fragment的管理類
ft = fm.beginTransaction(); // 注意:任何FragmentLayout操作Fragment的類都需要在一個事務中進行(相信如果有數據庫基礎的同學應該很好理解。
ft.replace(R.id.id_fragment, fragments.get(0)); // 替換方法,代表將FragmentLayout組件使用一個Fragment類進行替換掉(注意第一個參數就是FagmentLayout
監聽按鈕單擊事件
<span style="white-space:pre"> </span>public void btnStartClick(View v) {
switch (v.getId()) {
case R.id.id_btn1:
ft = fm.beginTransaction();
ft.replace(R.id.id_fragment, fragments.get(0));
ft.commit();
break;
case R.id.id_btn2:
ft = fm.beginTransaction();
ft.replace(R.id.id_fragment, fragments.get(1));
ft.commit();
break;
}
}