前言
椒江網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián)建站,椒江網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為椒江上1000家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\外貿(mào)網(wǎng)站制作要多少錢(qián),請(qǐng)找那個(gè)售后服務(wù)好的椒江做網(wǎng)站的公司定做!
Android運(yùn)行在各種各樣的設(shè)備中,有小屏幕的手機(jī),超大屏的平板甚至電視。針對(duì)屏幕尺寸的差距,很多情況下,都是先針對(duì)手機(jī)開(kāi)發(fā)一套App,然后拷貝一份,修改布局以適應(yīng)平板神馬超級(jí)大屏的。難道無(wú)法做到一個(gè)App可以同時(shí)適應(yīng)手機(jī)和平板么,當(dāng)然了,必須有啊。Fragment的出現(xiàn)就是為了解決這樣的問(wèn)題。
如今市面上的應(yīng)用基本上都是單Activity+多Fragment實(shí)現(xiàn)的了,而這類APP都有在相互切換時(shí)不被回收,即切換回原來(lái)的Fragment時(shí)還是原先的狀態(tài),這就是這里要實(shí)現(xiàn)的了。
這里使用Fragment的add()
、show()
、hide()
實(shí)現(xiàn),即顯示和隱藏,這樣原來(lái)的Fragment就不會(huì)被銷毀了。
二話不說(shuō),貼代碼,代碼是最好的老師。
示例代碼(注釋還算詳細(xì)了)
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; // 記錄當(dāng)前顯示的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); // 初始化首次進(jìn)入時(shí)的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()){ // 沒(méi)有添加過(guò): // 隱藏當(dāng)前的,添加新的,顯示新的 transaction.hide(mCurrentFragmen).add(R.id.fl_show, to, tag).show(to); }else{ // 隱藏當(dāng)前的,顯示新的 transaction.hide(mCurrentFragmen).show(to); } mCurrentFragmen = to; transaction.commitAllowingStateLoss(); } } // 當(dāng)activity非正常銷毀時(shí)被調(diào)用 @Override public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) { super.onSaveInstanceState(outState, outPersistentState); // 重置Fragment,防止當(dāng)內(nèi)存不足時(shí)導(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{ // 通過(guò)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); } } }
我以前對(duì)于這種需求是在一個(gè)Activity中使用RelativeLayout,在其中加入多個(gè)布局(類似Fragment),當(dāng)點(diǎn)擊下方Tab時(shí)設(shè)置布局的visibility的,思想是一樣的,但這樣實(shí)現(xiàn)起來(lái)很是丑陋,所以不建議使用。
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)創(chuàng)新互聯(lián)的支持