Android(Java):fragment

快速來回切換會產生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();
  }
 }

發佈了51 篇原創文章 · 獲贊 2 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章