使用ViewPager切換Fragment時,防止頻繁調用OnCreatView,fragmentoncreat

來源:互聯網
上載者:User

使用ViewPager切換Fragment時,防止頻繁調用OnCreatView,fragmentoncreat

使用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導致頻繁調用oncreatview15             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     @Override32     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=" + object37                 + " v=" + ((Fragment)object).getView());38         //該處使用detach導致頻繁調用oncreatview39         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

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.