前言
一般频繁切换Fragment会导致频繁的释放和创建,如果Fragment比较臃肿体验就非常不好了,这里分享一个方法。
正文
一、应用场景
1、不使用VIEwPager
2、不能用replace来切换Fragment,会导致Fragment释放(调用onDestroyVIEw)
二、实现
1、xml
<linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:orIEntation="vertical" > <FrameLayout androID:ID="@+ID/container" androID:layout_wIDth="match_parent" androID:layout_height="0dip" androID:layout_weight="1.0" > </FrameLayout> <RadioGroup androID:ID="@+ID/main_radio" androID:layout_wIDth="fill_parent" androID:layout_height="wrap_content" androID:layout_gravity="bottom" androID:gravity="bottom" androID:layout_marginBottom="-6dp" androID:orIEntation="horizontal" > <Radiobutton androID:ID="@+ID/radio_button0" androID:drawabletop="@drawable/bottom_1" /> <Radiobutton androID:ID="@+ID/radio_button1" androID:drawabletop="@drawable/bottom_2" /> <Radiobutton androID:ID="@+ID/radio_button2" androID:drawabletop="@drawable/bottom_3" /> <Radiobutton androID:ID="@+ID/radio_button3" androID:drawabletop="@drawable/bottom_4" /> <Radiobutton androID:ID="@+ID/radio_button4" androID:drawabletop="@drawable/bottom_5" /> </RadioGroup></linearLayout>
代码说明:
非常常见的底部放5个Radiobutton,点击切换不同的Fragment。
2、Activity
为Radiobutton设置setonCheckedchangelistener事件,其他代码:
public voID onCheckedChanged(Compoundbutton buttonVIEw,boolean isChecked) { if (isChecked) { Fragment fragment = (Fragment) mFragmentPagerAdapter.instantiateItem(mContainer,buttonVIEw.getID()); mFragmentPagerAdapter.setPrimaryItem(mContainer,fragment); mFragmentPagerAdapter.finishUpdate(mContainer); } } private FragmentPagerAdapter mFragmentPagerAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) { @OverrIDe public Fragment getItem(int position) { switch (position) { case R.ID.radio_button1: return new Fragment1(); case R.ID.radio_button2: return new Fragment2(); case R.ID.radio_button3: return new Fragment3(); case R.ID.radio_button4: return new Fragment4(); case R.ID.radio_button0: default: return new Fragment0(); } } @OverrIDe public int getCount() { return 5; } };
代码说明:
instantiateItem从FragmentManager中查找Fragment,找不到就getItem新建一个,setPrimaryItem设置隐藏和显示,最后finishUpdate提交事务, mContainer就是xml中的FrameLayout。
三、FragmentPagerAdapter核心代码
@OverrIDe public Object instantiateItem(VIEwGroup container,int position) { if (mCurTransaction == null) { mCurTransaction = mFragmentManager.beginTransaction(); } final long itemID = getItemID(position); // Do we already have this fragment? String name = makeFragmentname(container.getID(),itemID); Fragment fragment = mFragmentManager.findFragmentByTag(name); if (fragment != null) { if (DEBUG) Log.v(TAG,"Attaching item #" + itemID + ": f=" + fragment); mCurTransaction.attach(fragment); } else { fragment = getItem(position); if (DEBUG) Log.v(TAG,"Adding item #" + itemID + ": f=" + fragment); mCurTransaction.add(container.getID(),fragment,makeFragmentname(container.getID(),itemID)); } if (fragment != mCurrentPrimaryItem) { fragment.setMenuVisibility(false); fragment.setUserVisibleHint(false); } return fragment; } @OverrIDe public voID destroyItem(VIEwGroup container,int position,Object object) { if (mCurTransaction == null) { mCurTransaction = mFragmentManager.beginTransaction(); } if (DEBUG) Log.v(TAG,"Detaching item #" + getItemID(position) + ": f=" + object + " v=" + ((Fragment)object).getVIEw()); mCurTransaction.detach((Fragment)object); } @OverrIDe public voID setPrimaryItem(VIEwGroup container,Object object) { Fragment fragment = (Fragment)object; if (fragment != mCurrentPrimaryItem) { if (mCurrentPrimaryItem != null) { mCurrentPrimaryItem.setMenuVisibility(false); mCurrentPrimaryItem.setUserVisibleHint(false); } if (fragment != null) { fragment.setMenuVisibility(true); fragment.setUserVisibleHint(true); } mCurrentPrimaryItem = fragment; } } @OverrIDe public voID finishUpdate(VIEwGroup container) { if (mCurTransaction != null) { mCurTransaction.commitAllowingStateLoss(); mCurTransaction = null; mFragmentManager.executePendingTransactions(); } public voID setMenuVisibility(boolean menuVisible) {
super.setMenuVisibility(menuVisible);
if (this.getVIEw() != null)
this.getVIEw().setVisibility(menuVisible ? VIEw.VISIBLE : VIEw.GONE);
} }
FragmentPagerAdapter是support包自带的类。
四、注意
之前自己模拟VIEwPager用attach、setMenuVisibility、setUserVisibleHint来控制Fragment的显示隐藏,经常会出现Fragment重叠现象,非常头疼,换了这个之后目前没有发现重叠现象。
结束:以上就是对AndroID Fragment 实例说明,希望能帮助有需要的同学。
总结以上是内存溢出为你收集整理的Android 保存Fragment 切换状态实例代码全部内容,希望文章能够帮你解决Android 保存Fragment 切换状态实例代码所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)