前言
成都創(chuàng)新互聯(lián)公司是一家專業(yè)提供達坂城企業(yè)網(wǎng)站建設(shè),專注與網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計、H5建站、小程序制作等業(yè)務(wù)。10年已為達坂城眾多企業(yè)、政府機構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站制作公司優(yōu)惠進行中。Android運行在各種各樣的設(shè)備中,有小屏幕的手機,超大屏的平板甚至電視。針對屏幕尺寸的差距,很多情況下,都是先針對手機開發(fā)一套App,然后拷貝一份,修改布局以適應(yīng)平板神馬超級大屏的。難道無法做到一個App可以同時適應(yīng)手機和平板么,當然了,必須有啊。Fragment的出現(xiàn)就是為了解決這樣的問題。
如今市面上的應(yīng)用基本上都是單Activity+多Fragment實現(xiàn)的了,而這類APP都有在相互切換時不被回收,即切換回原來的Fragment時還是原先的狀態(tài),這就是這里要實現(xiàn)的了。
這里使用Fragment的add()
、show()
、hide()
實現(xiàn),即顯示和隱藏,這樣原來的Fragment就不會被銷毀了。
二話不說,貼代碼,代碼是最好的老師。
示例代碼(注釋還算詳細了)
public class MainActivity extends AppCompatActivity implements View.OnClickListener { private ImageView ibOne; private ImageView ibTwo; private ImageView ibThree; private FragmentManager mFm; private ArrayListmFragmentList = new ArrayList (); private String[] mFragmentTagList = {"OneFragment", "TwoFragment", "ThreeFragment"}; private Fragment mCurrentFragmen = null; // 記錄當前顯示的Fragment @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); initData(); } private void initData() { OneFragment oneFragment = new OneFragment(); TwoFragment twoFragment = new TwoFragment(); ThreeFragment threeFragment = new ThreeFragment(); mFragmentList.add(0, oneFragment); mFragmentList.add(1, twoFragment); mFragmentList.add(2, threeFragment); mCurrentFragmen = mFragmentList.get(0); // 初始化首次進入時的Fragment mFm = getFragmentManager(); FragmentTransaction transaction = mFm.beginTransaction(); transaction.add(R.id.fl_show, mCurrentFragmen, mFragmentTagList[0]); transaction.commitAllowingStateLoss(); } // findViewById private void initView() { ibOne = (ImageView)findViewById(R.id.ib_one); ibTwo = (ImageView)findViewById(R.id.ib_two); ibThree = (ImageView)findViewById(R.id.ib_three); ibOne.setOnClickListener(this); ibTwo.setOnClickListener(this); ibThree.setOnClickListener(this); } @Override public void onClick(View view) { switch (view.getId()){ case R.id.ib_one: switchFragment(mFragmentList.get(0), mFragmentTagList[0]); break; case R.id.ib_two: switchFragment(mFragmentList.get(1), mFragmentTagList[1]); break; case R.id.ib_three: switchFragment(mFragmentList.get(2), mFragmentTagList[2]); break; } } // 轉(zhuǎn)換Fragment void switchFragment(Fragment to, String tag){ if(mCurrentFragmen != to){ FragmentTransaction transaction = mFm.beginTransaction(); if(!to.isAdded()){ // 沒有添加過: // 隱藏當前的,添加新的,顯示新的 transaction.hide(mCurrentFragmen).add(R.id.fl_show, to, tag).show(to); }else{ // 隱藏當前的,顯示新的 transaction.hide(mCurrentFragmen).show(to); } mCurrentFragmen = to; transaction.commitAllowingStateLoss(); } } // 當activity非正常銷毀時被調(diào)用 @Override public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) { super.onSaveInstanceState(outState, outPersistentState); // 重置Fragment,防止當內(nèi)存不足時導(dǎo)致Fragment重疊 updateFragment(outState); } // 重置Fragment private void updateFragment(Bundle outState) { mFm = getFragmentManager(); if(outState == null){ FragmentTransaction transaction = mFm.beginTransaction(); OneFragment oneFragment = new OneFragment(); mCurrentFragmen = oneFragment; transaction.add(R.id.fl_show, oneFragment, mFragmentTagList[0]).commitAllowingStateLoss(); }else{ // 通過tag找到fragment并重置 OneFragment oneFragment = (OneFragment) mFm.findFragmentByTag(mFragmentTagList[0]); TwoFragment twoFragment = (TwoFragment) mFm.findFragmentByTag(mFragmentTagList[1]); ThreeFragment threeFragment = (ThreeFragment) mFm.findFragmentByTag(mFragmentTagList[2]); mFm.beginTransaction().show(oneFragment).hide(twoFragment).hide(threeFragment); } } }