• 使用ViewPager切换Fragment时,防止频繁调用OnCreatView


    使用ViewPager切换Fragment,我原先使用系统自带的适配器FragmentPagerAdapter。

    切换fragment时,频繁调用oncreatview()。

    查看FragmentPagerAdapter的源码,发现两个关键的地方

     1 @Override
     2     public Object instantiateItem(ViewGroup container, int position) {
     3         if (mCurTransaction == null) {
     4             mCurTransaction = mFragmentManager.beginTransaction();
     5         }
     6 
     7         final long itemId = getItemId(position);
     8 
     9         // Do we already have this fragment?
    10         String name = makeFragmentName(container.getId(), itemId);
    11         Fragment fragment = mFragmentManager.findFragmentByTag(name);
    12         if (fragment != null) {
    13             if (DEBUG) Log.v(TAG, "Attaching item #" + itemId + ": f=" + fragment);
    14            //该处使用attach导致频繁调用oncreatview
    15             mCurTransaction.attach(fragment);
    16 
    17         } else {
    18             fragment = getItem(position);
    19             if (DEBUG) Log.v(TAG, "Adding item #" + itemId + ": f=" + fragment);
    20             mCurTransaction.add(container.getId(), fragment,
    21                     makeFragmentName(container.getId(), itemId));
    22         }
    23         if (fragment != mCurrentPrimaryItem) {
    24             fragment.setMenuVisibility(false);
    25             fragment.setUserVisibleHint(false);
    26         }
    27 
    28         return fragment;
    29     }
    30 
    31     @Override
    32     public void destroyItem(ViewGroup container, int position, Object object) {
    33         if (mCurTransaction == null) {
    34             mCurTransaction = mFragmentManager.beginTransaction();
    35         }
    36         if (DEBUG) Log.v(TAG, "Detaching item #" + getItemId(position) + ": f=" + object
    37                 + " v=" + ((Fragment)object).getView());
    38         //该处使用detach导致频繁调用oncreatview
    39         mCurTransaction.detach((Fragment)object);
    40     }

    attach和detach的频繁使用导致了fragment频繁调用oncreatview。

    找到元凶了,接下来就好办了。

    自定义一个适配器,将attach改为show,将detach改为hide。

    完美解决问题。

    @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);
                //用show而不用attach,防止频繁调用oncreatview
                mCurTransaction.show(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());
            //用hide而不用detach,防止频繁调用oncreatview
            mCurTransaction.hide((Fragment) object);
        }
    View Code
  • 相关阅读:
    NHibernate 入门必看——NHibernate Made Simple
    ASP.NET 的多线程
    asp.net 禁止用户二次登录(转)
    marquee标记用法及在asp.net中的应用(转)
    解决Visual Studio 2005显示中文乱码(zhuan)
    ms sql 触发器( 转)
    Asp.net 页面导航的几种方法与比较
    ASP.NET1.1(VB):DataGrid中"加入序号列"和"截取定长字符串追加'...
    解决“Internet Explorer 无法打开 Internet站点已终止操作”问题(转)
    ASP.NET 2.0的页面指令集(转)
  • 原文地址:https://www.cnblogs.com/shenchanghui/p/6094703.html
Copyright © 2020-2023  润新知