FragmentActivity + Fragment(通过hide和show来显示fragment) + Fragment(通过viewpager来显示fragment)
在Activity中
//开启事务 FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); //添加首页 ft.add(R.id.content,network_fragment).commit();
FragmentManager fragmentManager = getSupportFragmentManager(); FragmentTransaction ft = fragmentManager.beginTransaction(); //隐藏 ft.hide(mFragments[mIndex]); //判断是否添加 if(!mFragments[index].isAdded()){ ft.add(R.id.content,mFragments[index]).show(mFragments[index]); }else { ft.show(mFragments[index]); } ft.commit();
在布局中
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <FrameLayout android:id="@+id/content" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginStart="@dimen/one_hundred_sixty" android:background="@color/mx_plus_background" /> <include android:id="@+id/id_ly_bottombar" android:layout_width="wrap_content" android:layout_height="match_parent" layout="@layout/leftbottombar" /> </FrameLayout>
然后在fragment中会添加一个viewpager来再添加几个fragment
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/main_back" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <com.roadrover.libaudiosettings.widget.InterceptTouchViewPager android:id="@+id/id_content" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="@dimen/sixty_px" android:background="@drawable/content_bottom"/> <include android:id="@+id/id_ly_bottombar" layout="@layout/bottombar" android:layout_width="match_parent" android:layout_height="wrap_content" /> </FrameLayout>
在fragment中的ViewPager在添加新的fragment,
设置参数的时候最好不要用setTargetFragment,自己定义一个set方法(比如setInitFragment)就可以了,
设置manager时,要用getChildFragmentManager(fragment+fragment用这个),不能用getSupportFragmentManager(activity+fragment才用这个)
这是为了避免在我切换语言之后,viewpager中的fragment显示不出来的bug。
mSpeakerFragment = new SpeakerFragment(); mSpeakerFragment.setInitFragment(EffectFragment.this); //不能直接设置fragment,耦合性太强,要使用接口回调 mAdapter = new FragmentPagerAdapter(getChildFragmentManager()) { // @Override public Fragment getItem(int arg0) { Logcat.d(TAG, "getItem = " + arg0); return mFragments.get(arg0); } @Override public int getCount() { return mFragments.size(); } };
FragmentStatePagerAdapter getChildFragmentManager
----------------------------------------------------------
不要使用有参构造的fragment,不要使用setTargetFragment,
最好能不在fragment的onCreateView中new对象,因为在viewpager中,只会保持fragment一前一后,超出了,就会走onDestroyView(onDestroy不走)。
----------------------------------------------------------
/** * 状态检测 用于内存不足的时候保证fragment不会重叠 * * @param savedInstanceState */ private void stateCheck(Bundle savedInstanceState) { if (savedInstanceState == null) { fm = getFragmentManager(); FragmentTransaction fts = fm.beginTransaction(); AnimationFragment af = new AnimationFragment(); mContent = af; fts.add(R.id.content_frame, af); fts.commit(); } else { AnimationFragment af = (AnimationFragment) getFragmentManager() .findFragmentByTag(tags[0]); PlainFragment pf = (PlainFragment) getFragmentManager() .findFragmentByTag(tags[1]); RecordFragment rf = (RecordFragment) getFragmentManager() .findFragmentByTag(tags[2]); InformationFragment inf = (InformationFragment) getFragmentManager() .findFragmentByTag(tags[3]); TestingFragment tf = (TestingFragment) getFragmentManager() .findFragmentByTag(tags[4]); getFragmentManager().beginTransaction().show(af).hide(pf).hide(rf) .hide(inf).hide(tf).commit(); } }