首先是創建一個項目,然後MainActivity繼承FragmentActivity,配置好佈局文件,再切換到MainFragment,其中MainFragment是一個過渡。
<?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="@color/white"
android:orientation="vertical" >
<View
android:id="@+id/statusBarBackground"
android:layout_width="match_parent"
android:layout_height="0dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:orientation="vertical" >
<!-- The main content view -->
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
MainActivity中佈局文件中的content_frame是用來替換MainFragment的。
public class MainActivity extends FragmentActivity {
private MainFragment fgt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);//佈局文件
switchFgt();//切換到MainFragment
}
private void switchFgt() {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
fgt = null;
fgt = new MainFragment();
ft.replace(R.id.content_frame, fgt);
ft.commit();
}
}
接下來就是MainFragment,首先佈局文件分爲兩部分:導航欄部分(選擇要跳轉的Fragment,帶有兩個RadioButton的LinearLayout);fragment顯示部分(對應FrameLayout)。
<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:background="@color/white"
android:orientation="vertical" >
<FrameLayout
android:id="@+id/fgt"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1" >
</FrameLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bottom_bar_bg"
android:gravity="center_vertical"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/rbMap"
style="@style/mainBottomRadioButton"
android:drawableTop="@drawable/rb_map_selector"
android:text="附近的人" />
<RadioButton
android:id="@+id/rbMine"
style="@style/mainBottomRadioButton"
android:drawableTop="@drawable/rb_mine_selector"
android:text="我的" />
</LinearLayout>
</LinearLayout>
注意fragment配置佈局文件的方法,onCreateView方法返回一個帶有佈局文件的View顯示。View view = inflater.inflate(R.layout.fragment_main, null);。獲取fragment的管理器,再根據用戶的選擇(監聽RadioButton)跳轉到相應的Fragment去顯示。
public class MainFragment extends Fragment{
private FragmentManager fragmentManager;
private RadioButton rbMap;
private RadioButton rbMine;
private CompoundButton lastChecked;// 上一次選中的按鈕
private MapFragment mMapFgt;
private MineFragment mMineFgt;
public Fragment curShowFragment = null;
@Override
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main, null);
fragmentManager = getChildFragmentManager();
rbMap=(RadioButton) view.findViewById(R.id.rbMap);
rbMine=(RadioButton) view.findViewById(R.id.rbMine);
rbMap.setChecked(true);
lastChecked=rbMap;
//以我的模塊舉例:附近的人模塊類似
rbMine.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
checkedChanged(buttonView,isChecked);
}
});
return view;
}
//選擇之後觸發,選中重置上次選項的值,再調用switchFragmentByRbtnId方法
public void checkedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
if (lastChecked != null) {
lastChecked.setChecked(false);
}
switchFragmentByRbtnId(buttonView.getId());
lastChecked = buttonView;
}
}
//根據選中項ID顯示相應fragment
private void switchFragmentByRbtnId(int checkedId) {
FragmentTransaction ft = fragmentManager.beginTransaction();
//確定選中的ID
String tag = String.valueOf(checkedId);
Fragment showFragment = fragmentManager.findFragmentByTag(tag);
boolean needAddFragment = showFragment == null ? true : false;
switch (checkedId) {//以我的模塊舉例:附近的人模塊類似
case R.id.rbMine:
if (showFragment == null) {
//設置顯示的fragment,當前爲MineFragment
mMineFgt = new MineFragment();
showFragment = mMineFgt;
}
break;
}
if (needAddFragment) {
ft.add(R.id.fgt, showFragment, tag);
}
// 同一個rb多次觸發不再調用事務
if (showFragment != curShowFragment) {
if (curShowFragment != null) {
ft.hide(curShowFragment);
}
if (showFragment != null) {
// ft.replace(R.id.fgt, showFragment);
ft.show(showFragment);
curShowFragment = showFragment;
}
}
ft.commit();
}
}
最後MineFragment中在設置佈局文件,顯示相應的內容即可。
小結
其實Fragment最主要的還是以上兩個部分,掌握了就差不多了。