【Android】儲存Fragment切換狀態

來源:互聯網
上載者:User

標籤:android   des   style   blog   http   使用   os   strong   

【Android】儲存Fragment切換狀態

前言

一般頻繁切換Fragment會導致頻繁的釋放和建立,如果Fragment比較臃腫體驗就非常不好了,這裡分享一個方法。

 

聲明

歡迎轉載,但請保留文章原始出處:) 
部落格園:http://www.cnblogs.com
農民伯伯: http://over140.cnblogs.com 

 

本文

一、應用情境

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禁用掉左右滑動和自動銷毀即可,根據評論來看非正常情況下重影現象還是挺嚴重的。 

 

結束

需要多看看源碼,才能很好的解決問題。 

 

http://www.cnblogs.com/over140/p/3362047.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.