美文网首页Android开发之路
ViewPager用FragmentPageAdapter实现相

ViewPager用FragmentPageAdapter实现相

作者: phoenixsky | 来源:发表于2016-12-01 16:09 被阅读92次

    发现问题 (FragmentPageAdapter)

    因为FragmentPageAdapter在超出缓存的页面后,只是将fragment中的mView设置为空,但是fragment对象的引用还在,由于我使用了ButterKnife,在fragment的对象中有个imageView的成员变量,导致imageView无法回收.而如果使用了FragmentStatePagerAdapter,则会将fragment的直接置空,不会出现内存释放不了的问题

    FragmentPageAdapter 与FragmentStatePageAdapter的区别

    FragmentPageAdapter的源码

    @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);//因为fragment实例没有被真正释放,所以可以直接attach效率高
          } else {
              fragment = getItem(position);//初始化页面的时候拿到fragment的实例
              if (DEBUG) Log.v(TAG, "Adding item #" + itemId + ": f=" + fragment);
              mCurTransaction.add(container.getId(), fragment,
                      makeFragmentName(container.getId(), itemId));//add上去
          }
          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);//并没有真正释放fragment对象只是detach
      }
    

    FragmentStatePageAdapter的destroy

    @Override
      public Object instantiateItem(ViewGroup container, int position) {
          // If we already have this item instantiated, there is nothing
          // to do.  This can happen when we are restoring the entire pager
          // from its saved state, where the fragment manager has already
          // taken care of restoring the fragments we previously had instantiated.
          if (mFragments.size() > position) {
              Fragment f = mFragments.get(position);//fragment被释放后这里得到的null值
              if (f != null) {
                  return f;
              }
          }
    
          if (mCurTransaction == null) {
              mCurTransaction = mFragmentManager.beginTransaction();
          }
    
          Fragment fragment = getItem(position);//fragment被释放后或者是初次进入页面拿到新的Fragment实例
          if (DEBUG) Log.v(TAG, "Adding item #" + position + ": f=" + fragment);
          if (mSavedState.size() > position) {
              Fragment.SavedState fss = mSavedState.get(position);
              if (fss != null) {
                  fragment.setInitialSavedState(fss);
              }
          }
          while (mFragments.size() <= position) {
              mFragments.add(null);
          }
          fragment.setMenuVisibility(false);
          fragment.setUserVisibleHint(false);
          mFragments.set(position, fragment);
          mCurTransaction.add(container.getId(), fragment);//新的Fragment实例 是add上去的
    
          return fragment;
      }
    
     @Override
      public void destroyItem(ViewGroup container, int position, Object object) {
          Fragment fragment = (Fragment) object;
    
          if (mCurTransaction == null) {
              mCurTransaction = mFragmentManager.beginTransaction();
          }
          if (DEBUG) Log.v(TAG, "Removing item #" + position + ": f=" + object
                  + " v=" + ((Fragment)object).getView());
          while (mSavedState.size() <= position) {
              mSavedState.add(null);
          }
          mSavedState.set(position, fragment.isAdded()
                  ? mFragmentManager.saveFragmentInstanceState(fragment) : null);
          mFragments.set(position, null);//真正释放了fragment实例
    
          mCurTransaction.remove(fragment);
      }
    

    引用ViewPager 全面剖析及使用详解 - 简书

    FragmentPageAdapter解决方案

    在Fragment-->onDestroyView()的时候,将含有bitmap的imageView移除.

    ((ViewGroup) imageView.getParent()).removeView(imageView);
    

    引用自 android - FragmentStatePagerAdapter OutOfMemoryError - Stack Overflow

    protected void onDestroy(){
      super.onDestroy();   
      unbindDrawables(getView()); // <---This should be the ID of this fragments (ScreenSlidePageFragment) layout
    }
    private void unbindDrawables(View view){ 
      if (view.getBackground() != null)   { 
         view.getBackground().setCallback(null);  
       }   
      if (view instanceof ViewGroup && !(view instanceof AdapterView))   { 
           for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++)      {
               unbindDrawables(((ViewGroup) view).getChildAt(i));      
          }
          ((ViewGroup) view).removeAllViews(); 
      }
    }
    

    相关文章

      网友评论

        本文标题:ViewPager用FragmentPageAdapter实现相

        本文链接:https://www.haomeiwen.com/subject/kmwxmttx.html