快速来回切换会产生java.lang.IllegalStateException: Fragment already added问题,现在改成左侧功能菜单的处理方式,使用fragment的show hide方法。

<?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:orientation="horizontal" >
<LinearLayout
android:id="@+id/setting_item_container"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="3"
android:orientation="vertical"
android:background="@color/mycourse_group_unselected_bg"
/>
<FrameLayout
android:id="@+id/setting_detail"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="@android:color/white"
>
<fragment
android:name="com.dongao.mainclient.pad.fragment.BasicSettingFragment"
android:id="@+id/basicfragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<fragment
android:name="com.dongao.mainclient.pad.fragment.CommonProblemFragment"
android:id="@+id/problemfragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<fragment
android:name="com.dongao.mainclient.pad.fragment.FeedbackFragment"
android:id="@+id/feedfragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<fragment
android:name="com.dongao.mainclient.pad.fragment.RecordFragment"
android:id="@+id/recordfragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</FrameLayout>
</LinearLayout>

private FragmentManager fragmentManager;
private Fragment basicSettingFragment;
private Fragment commonProblemFragment;
private Fragment feedbackFragment;
private Fragment recordFragment;

fragmentManager = getFragmentManager();
basicSettingFragment = fragmentManager.findFragmentById(R.id.basicfragment);
commonProblemFragment = fragmentManager.findFragmentById(R.id.problemfragment);
feedbackFragment = fragmentManager.findFragmentById(R.id.feedfragment);
recordFragment = fragmentManager.findFragmentById(R.id.recordfragment);
fragmentManager.beginTransaction().hide(commonProblemFragment).commit();
fragmentManager.beginTransaction().hide(feedbackFragment).commit();
fragmentManager.beginTransaction().hide(recordFragment).commit();

private class MyOnclickListener implements OnClickListener{
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch (arg0.getId()) {
case BASICSETTING:
FragmentTransaction transaction_BasicSetting = fragmentManager.beginTransaction();
transaction_BasicSetting.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out);
transaction_BasicSetting.show(basicSettingFragment);
transaction_BasicSetting.hide(commonProblemFragment);
transaction_BasicSetting.hide(feedbackFragment);
transaction_BasicSetting.hide(recordFragment);
transaction_BasicSetting.commit();
break;
case COMMONPROBLEM:
FragmentTransaction transaction_CommonPro = fragmentManager.beginTransaction();
transaction_CommonPro.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out);
transaction_CommonPro.hide(basicSettingFragment);
transaction_CommonPro.show(commonProblemFragment);
transaction_CommonPro.hide(feedbackFragment);
transaction_CommonPro.hide(recordFragment);
transaction_CommonPro.commit();
break;
case FEEDBACK:
FragmentTransaction transaction_Feedback = fragmentManager.beginTransaction();
transaction_Feedback.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out);
transaction_Feedback.hide(basicSettingFragment);
transaction_Feedback.hide(commonProblemFragment);
transaction_Feedback.show(feedbackFragment);
transaction_Feedback.hide(recordFragment);
transaction_Feedback.commit();
break;
case RECORD_LEARNING:
FragmentTransaction transaction_Reord = fragmentManager.beginTransaction();
transaction_Reord.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out);
transaction_Reord.hide(basicSettingFragment);
transaction_Reord.hide(commonProblemFragment);
transaction_Reord.hide(feedbackFragment);
transaction_Reord.show(recordFragment);
transaction_Reord.commit();
break;
default:
break;
}
setButtonState(arg0);
}
}

@Override
public void onHiddenChanged(boolean hidden) {
// TODO Auto-generated method stub
Log.d("onHiddenChanged", "onHiddenChanged");
super.onHiddenChanged(hidden);
if(!hidden){
initData();
}
}

更多相关文章

  1. Android 菜单项选项
  2. android 自定义menu菜单按键功能
  3. 【移动开发】Android中的底部菜单框架(Fragment)
  4. android adt安装成功后 eclipse菜单栏没有相应的图标的解决方法

随机推荐

  1. Android(安卓)- listview未滚动触发onScr
  2. Android控件之Textiew的使用
  3. 横竖屏切换和contentView的三种显示方式
  4. 打电话
  5. android开机自启动的后台Service的实现
  6. android Configuration详解
  7. ImageView的属性android:scaleType,即Imag
  8. Android中build target,minSdkVersion,targ
  9. cygwin配置swig开发环境(Android)
  10. 翻译 Migrating to Android(安卓)Studio