防止ViewPager中的Fragment被销毁的方法

在使用ViewPager与Fragment的时候,ViewPager会自动缓存1页内的数据,如下图:


当我们当前处在页面2的时候,页面1和页面3的View实际上已经创建好了,所以在我们拖动的时候是可以看见他们的界面的。

但是当我们的页面处在1的时候,页面3实际上就已经销毁了。直到跳转到页面2的时候,页面3才会创建View。


这时,如果页面3中有需要耗时的事件,比如网络访问。那么,在我们进行 1-->2 的操作的时候,就会不断的出现页面3加载的对话框(如果有的话)。而且如果快速的 1-->2-->3的切换,3中的内容很可能还没加载出来。

这样重复的加载,既影响体验、又耗费时间和流量,所以笔者这两天一直在查如何在Fragment移出的时候不要销毁,或者保存状态。

后来发现真是多此一举,如果您的软件对内存消耗不是很在意的话,只需加入以下代码:

 
pager.setOffscreenPageLimit(2);


 

 

 

pager.setOffscreenPageLimit(2);
就可以让ViewPager多缓存一个页面,这样上面的问题就得到了解决。


当然这只是个取巧的方法,如果有比较好的保存状态的方法会更好。


转载请注明来自:http://blog.csdn.net/icyfox_bupt/article/details/18356461

处理方法二。

在项目中需要进行Fragment的切换,一直都是用replace()方法来替换Fragment:

Java代码 复制代码  收藏代码
  1. public void switchContent(Fragment fragment) {  
  2.        if(mContent != fragment) {  
  3.            mContent = fragment;  
  4.            mFragmentMan.beginTransaction()  
  5.                .setCustomAnimations(android.R.anim.fade_in, R.anim.slide_out)  
  6.                .replace(R.id.content_frame, fragment) // 替换Fragment,实现切换  
  7.                .commit();  
  8.        }  
  9.    }  
 public void switchContent(Fragment fragment) {
        if(mContent != fragment) {
            mContent = fragment;
            mFragmentMan.beginTransaction()
                .setCustomAnimations(android.R.anim.fade_in, R.anim.slide_out)
                .replace(R.id.content_frame, fragment) // 替换Fragment,实现切换
                .commit();
        }
    }



但是,这样会有一个问题:
每次切换的时候,Fragment都会重新实例化,重新加载一边数据,这样非常消耗性能和用户的数据流量。

就想,如何让多个Fragment彼此切换时不重新实例化?

翻看了Android官方Doc,和一些组件的源代码,发现,replace()这个方法只是在上一个Fragment不再需要时采用的简便方法。

正确的切换方式是add(),切换时hide(),add()另一个Fragment;再次切换时,只需hide()当前,show()另一个。
这样就能做到多个Fragment切换不重新实例化:

Java代码 复制代码  收藏代码
  1. public void switchContent(Fragment from, Fragment to) {  
  2.         if (mContent != to) {  
  3.             mContent = to;  
  4.             FragmentTransaction transaction = mFragmentMan.beginTransaction().setCustomAnimations(  
  5.                     android.R.anim.fade_in, R.anim.slide_out);  
  6.             if (!to.isAdded()) {    // 先判断是否被add过  
  7.                 transaction.hide(from).add(R.id.content_frame, to).commit(); // 隐藏当前的fragment,add下一个到Activity中  
  8.             } else {  
  9.                 transaction.hide(from).show(to).commit(); // 隐藏当前的fragment,显示下一个  
  10.             }  
  11.         }  
  12.     }  

处理方法三---------------------------------------------

 

一、应用场景

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"
            style
="@style/main_tab_bottom"
            android:drawableTop
="@drawable/bottom_1"   />

         < RadioButton
            
android:id ="@+id/radio_button1"
            style
="@style/main_tab_bottom"
            android:drawableTop
="@drawable/bottom_2"   />

         < RadioButton
            
android:id ="@+id/radio_button2"
            style
="@style/main_tab_bottom"
            android:drawableTop
="@drawable/bottom_3"   />

         < RadioButton
            
android:id ="@+id/radio_button3"
            style
="@style/main_tab_bottom"
            android:drawableTop
="@drawable/bottom_4"   />

         < RadioButton
            
android:id ="@+id/radio_button4"
            style
="@style/main_tab_bottom"
            android:drawableTop
="@drawable/bottom_5"   />
     </ RadioGroup >

</ LinearLayout >
复制代码

代码说明:

非常常见的底部放5个RadioButton,点击切换不同的Fragment。

 

2、Activity

为RadioButton设置setOnCheckedChangeListener事件,其他代码:

复制代码
    @Override
     public  void onCheckedChanged(CompoundButton buttonView,  boolean isChecked) {
         if (isChecked) {
            Fragment fragment = (Fragment) mFragmentPagerAdapter.instantiateItem(mContainer, buttonView.getId());
            mFragmentPagerAdapter.setPrimaryItem(mContainer, 0, 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,  int position, 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();
        }
    }
复制代码

 FragmentPagerAdapter是support包自带的类。

 

四、注意 

之前自己模拟ViewPager用attach、setMenuVisibility、setUserVisibleHint来控制Fragment的显示隐藏,经常会出现Fragment重叠现象,非常头疼,换了这个之后目前没有发现重叠现象。

 

五、文章后期维护

2013-12-01  上传示例代码:http://files.cnblogs.com/over140/SampleFragmentSwitch.zip

 

    @Override
     public  void setMenuVisibility( boolean menuVisible) {
         super.setMenuVisibility(menuVisible);
         if ( this.getView() !=  null)
             this.getView().setVisibility(menuVisible ? View.VISIBLE : View.GONE);
    }

重新做例子时发现自己也出不来效果了,后来发现少了这段代码。

 

2014-01-08 想实现本文的效果还是推荐直接使用ViewPager,通过自定义ViewPager禁用掉左右滑动和自动销毁即可,根据评论来看非正常情况下重影现象还是挺严重的。 

 

结束

需要多看看源码,才能很好的解决问题。 

 

 

public void switchContent(Fragment from, Fragment to) {
        if (mContent != to) {
            mContent = to;
            FragmentTransaction transaction = mFragmentMan.beginTransaction().setCustomAnimations(
                    android.R.anim.fade_in, R.anim.slide_out);
            if (!to.isAdded()) {    // 先判断是否被add过
                transaction.hide(from).add(R.id.content_frame, to).commit(); // 隐藏当前的fragment,add下一个到Activity中
            } else {
                transaction.hide(from).show(to).commit(); // 隐藏当前的fragment,显示下一个
            }
        }
    }

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值