1.v4包的FragmentManager切换FrameLayout的页面
xml页:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/gray_bg" >
<com.ee.wedget.CustomActionBar
android:id="@+id/action_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" />
<FrameLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/horizontalScrollView1"
android:layout_below="@+id/action_bar" />
<HorizontalScrollView
android:id="@+id/horizontalScrollView1"
android:layout_width="wrap_content"
android:layout_height="52dip"
android:layout_alignParentBottom="true"
android:cacheColorHint="#00000000"
android:scrollbars="none" >
<RadioGroup
android:id="@+id/foot_nav_bar_rgroup"
android:layout_width="match_parent"
android:layout_height="52dip"
android:background="@drawable/foot_nav_bar_bg"
android:orientation="horizontal" >
</RadioGroup>
</HorizontalScrollView>
</RelativeLayout>
java代码:
void initListener() {
foot_nav_bar_rgroup
.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup arg0, int position) {
switch (position) {
case 1:
if (oneFragment == null) {
oneFragment = new TitleOneFragment();
}
mTitleBar.setTitleName("标题1");
hidebar();
switchFragment(oneFragment);
break;
case 2:
if (twoFragment == null) {
twoFragment = new TitleTwoFragment();
}
mTitleBar.setTitleName("标题2");
hidebar();
switchFragment(twoFragment);
break;
case 3:
if (threeFragment == null) {
threeFragment = new TitleThreeFragment();
}
mTitleBar.setTitleName("标题3");
hidebar();
switchFragment(threeFragment);
break;
case 4:
if (fourFragment == null) {
fourFragment = new TitleFourFragment();
}
mTitleBar.setTitleName("标题4");
switchFragment(fourFragment);
break;
default:
break;
}
}
});
foot_nav_bar_rgroup.check(1);
}
private void switchFragment(Fragment to) {
FragmentManager fragmentManager = getSupportFragmentManager();
if (currFragment != to) {
android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
if (!to.isAdded()) {
if (currFragment == null) {
fragmentTransaction.add(R.id.content, to).commit();
} else {
fragmentTransaction.hide(currFragment)
.add(R.id.content, to).commit();
}
} else {
fragmentTransaction.hide(currFragment).show(to).commit();
// 主动调用
to.onResume();
}
}
currFragment = to;
}