Android實(shí)現(xiàn)底部導(dǎo)航欄的主界面
在主流app中,應(yīng)用的主界面都是底部含有多個(gè)標(biāo)簽的導(dǎo)航欄,點(diǎn)擊可以切換到相應(yīng)的界面,如圖:
接下來將描述下其實(shí)現(xiàn)過程。
1.首先是分析界面,底部導(dǎo)航欄我們可以用一個(gè)占滿屏幕寬度、包裹著數(shù)個(gè)標(biāo)簽TextView、方向?yàn)闄M向horizontal的線性布局LinearLayout。上方則是一個(gè)占滿剩余空間的FrameLayout。
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" android:orientation="vertical"> <FrameLayout android:id="@+id/main_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1"/> <View android:layout_width="match_parent" android:layout_height="1px" android:background="@color/colorPrimaryDark" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:id="@+id/main_home" android:layout_width="wrap_content" android:layout_weight="1" android:layout_height="wrap_content" android:gravity="center" android:padding="20dp" android:text="首頁" /> <View android:layout_width="1px" android:layout_height="match_parent" android:background="@color/colorPrimaryDark" /> <TextView android:id="@+id/main_game" android:layout_width="wrap_content" android:layout_weight="1" android:layout_height="wrap_content" android:gravity="center" android:padding="20dp" android:text="游戲" /> <View android:layout_width="1px" android:layout_height="match_parent" android:background="@color/colorPrimaryDark" /> <TextView android:id="@+id/main_video" android:layout_width="wrap_content" android:layout_weight="1" android:layout_height="wrap_content" android:gravity="center" android:padding="20dp" android:text="視頻" /> <View android:layout_width="1px" android:layout_height="match_parent" android:background="@color/colorPrimaryDark" /> <TextView android:id="@+id/main_mine" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:padding="20dp" android:text="我的" /> </LinearLayout> </LinearLayout>
2.四個(gè)標(biāo)簽對應(yīng)四個(gè)Fragment,我們新建四個(gè)Fragment,布局放個(gè)TextView用于區(qū)分即可。
private HomeFragment homeFragment; private GameFragment gameFragment; private VideoFragment videoFragment; private MineFragment mineFragment;
3.打開MainActivity,首先初始化控件
private void initView() { home= findViewById(R.id.main_home); game= findViewById(R.id.main_game); video= findViewById(R.id.main_video); mine= findViewById(R.id.main_mine); layout= findViewById(R.id.main_layout); home.setOnClickListener(this); game.setOnClickListener(this); video.setOnClickListener(this); mine.setOnClickListener(this); }
onClick點(diǎn)擊事件放在后面處理
3.初始化四個(gè)fragment
我們初衷是讓fragment加載一次后,重復(fù)點(diǎn)擊或者切換回來都不會再加載以耗費(fèi)資源,這里常見的處理方式有viewpager的懶加載和fragment的hide、show,這里我們講解后者的實(shí)現(xiàn)方式。
private void initFragment() { FragmentTransaction transaction= getSupportFragmentManager().beginTransaction(); if (homeFragment!= null&& homeFragment.isAdded()){ transaction.remove(homeFragment); } if (gameFragment!= null&& gameFragment.isAdded()){ transaction.remove(gameFragment); } if (videoFragment!= null&& videoFragment.isAdded()){ transaction.remove(videoFragment); } if (mineFragment!= null&& mineFragment.isAdded()){ transaction.remove(mineFragment); } transaction.commitAllowingStateLoss(); homeFragment= null; gameFragment= null; videoFragment= null; mineFragment= null; home.performClick(); }
我們來逐行分析代碼
首先開啟一個(gè)事務(wù)
FragmentTransaction transaction= getSupportFragmentManager().beginTransaction();
進(jìn)行Fragment的非空處理
if (homeFragment!= null&& homeFragment.isAdded()){ transaction.remove(homeFragment); } if (gameFragment!= null&& gameFragment.isAdded()){ transaction.remove(gameFragment); } if (videoFragment!= null&& videoFragment.isAdded()){ transaction.remove(videoFragment); } if (mineFragment!= null&& mineFragment.isAdded()){ transaction.remove(mineFragment); } transaction.commitAllowingStateLoss();
將所有fragment設(shè)為null,自動執(zhí)行homefragment的點(diǎn)擊事件,即默認(rèn)顯示HomeFragment
homeFragment= null; gameFragment= null; videoFragment= null; mineFragment= null; home.performClick();
4.回到四個(gè)底部標(biāo)簽的點(diǎn)擊事件,我們執(zhí)行自定義的switchContent方法,將當(dāng)前點(diǎn)擊標(biāo)簽的view作為參數(shù)傳進(jìn)去
@Override public void onClick(View view) { switch (view.getId()){ case R.id.main_home: switchContent(home); break; case R.id.main_game: switchContent(game); break; case R.id.main_video: switchContent(video); break; case R.id.main_mine: switchContent(mine); break; } }
5.定位到switchContent方法
新建一個(gè)fragment對象,當(dāng)點(diǎn)擊某個(gè)標(biāo)簽時(shí),如果當(dāng)前fragment對象為空,就生成一個(gè)對應(yīng)fragment的對象實(shí)例
public void switchContent(View view){ Fragment fragment; if (view== home){ if (homeFragment== null){ homeFragment= new HomeFragment(); } fragment= homeFragment; }else if (view== game){ if (gameFragment== null){ gameFragment= new GameFragment(); } fragment= gameFragment; }else if (view== video){ if (videoFragment== null){ videoFragment= new VideoFragment(); } fragment= videoFragment; }else if (view== mine){ if (mineFragment== null){ mineFragment= new MineFragment(); } fragment= mineFragment; }else { return; }
生成對象后,我們就可以進(jìn)行fragment的添加顯示工作了。
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); if (mContent == null) { transaction.add(layout.getId(), fragment).commit(); mContent = fragment; } if (mContent != fragment) { if (!fragment.isAdded()) { transaction.hide(mContent).add(layout.getId(), fragment).commitAllowingStateLoss(); } else { transaction.hide(mContent).show(fragment).commitAllowingStateLoss(); } mContent = fragment; } home.setSelected(false); home.setSelected(false); home.setSelected(false); home.setSelected(false); view.setSelected(true);
分析這段代碼,我們主要是用當(dāng)前碎片mContent和上個(gè)碎片fragment做比較,這樣用來判斷底部導(dǎo)航欄是否點(diǎn)擊進(jìn)行了切換,首先當(dāng)應(yīng)用打開時(shí),因?yàn)槲覀兦懊嬲{(diào)用了第一個(gè)標(biāo)簽自動點(diǎn)擊方法。
home.performClick();
看流程,因?yàn)榇藭r(shí)mContent還為null,所以走這段代碼
if (mContent == null) { transaction.add(layout.getId(), fragment).commit(); mContent = fragment; }
此時(shí)fragment即為HomeFragment對象,頁面將顯示HomeFragment,并將該對象賦給mContent。
此時(shí)HomeFragment生命周期如下,從Attach()走到onResume(),一切正常。
接下來,點(diǎn)擊第二個(gè)標(biāo)簽,fragment為gameFragment,mContent為homeFragment。兩者不等,走這段方法。
if (mContent != fragment) { if (!fragment.isAdded()) { transaction.hide(mContent).add(layout.getId(), fragment).commitAllowingStateLoss(); } else { transaction.hide(mContent).show(fragment).commitAllowingStateLoss(); } mContent = fragment; }
分析代碼,GameFragment因?yàn)檫€沒被加載過,所以先走
transaction.hide(mContent).add(layout.getId(), fragment).commitAllowingStateLoss();
即隱藏掉mContent即HomeFragment,在將GameFragment加載顯示出來。
這時(shí)看GameFragment的生命周期,一切正常。
這時(shí)我們再點(diǎn)擊回HomeFragment,此時(shí)fragment為HomeFragment,mContent為GameFragment,同時(shí)HomeFragment因?yàn)楸籥dd過,所以走
transaction.hide(mContent).show(fragment).commitAllowingStateLoss();
這條語句指隱藏fragment同時(shí)不必加載add已經(jīng)加載過的fragment,直接show就可以,commitAllowingStateLoss方法與commit方法作用類似,更適用這種頻繁切換頁面下的提交工作,避免crash。
同時(shí)打開日志,發(fā)現(xiàn)HomeFragment并沒有被銷毀重載,這樣就達(dá)到了我們不想切換頻繁加載的目的。
至此全部完成,Demo附上!
資源下載
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
上一篇:使用RecyclerView實(shí)現(xiàn)水平列表
欄 目:Android
下一篇:RecyclerView仿應(yīng)用列表實(shí)現(xiàn)網(wǎng)格布局
本文標(biāo)題:Android實(shí)現(xiàn)底部導(dǎo)航欄的主界面
本文地址:http://mengdiqiu.com.cn/a1/Android/9195.html
您可能感興趣的文章
- 01-10Android自定義View之繪制圓形頭像功能
- 01-10Android實(shí)現(xiàn)雙擊返回鍵退出應(yīng)用實(shí)現(xiàn)方法詳解
- 01-10android實(shí)現(xiàn)記住用戶名和密碼以及自動登錄
- 01-10android實(shí)現(xiàn)簡單計(jì)算器功能
- 01-10Android 友盟第三方登錄與分享的實(shí)現(xiàn)代碼
- 01-10C++自定義API函數(shù)實(shí)現(xiàn)大數(shù)相乘算法
- 01-10如何給Flutter界面切換實(shí)現(xiàn)點(diǎn)特效
- 01-10android實(shí)現(xiàn)指紋識別功能
- 01-10Emoji表情在Android JNI中的兼容性問題詳解
- 01-10Android實(shí)現(xiàn)圓形漸變加載進(jìn)度條


閱讀排行
本欄相關(guān)
- 01-10Android自定義View之繪制圓形頭像功能
- 01-10Android實(shí)現(xiàn)雙擊返回鍵退出應(yīng)用實(shí)現(xiàn)方
- 01-10android實(shí)現(xiàn)簡單計(jì)算器功能
- 01-10android實(shí)現(xiàn)記住用戶名和密碼以及自動
- 01-10C++自定義API函數(shù)實(shí)現(xiàn)大數(shù)相乘算法
- 01-10Android 友盟第三方登錄與分享的實(shí)現(xiàn)代
- 01-10android實(shí)現(xiàn)指紋識別功能
- 01-10如何給Flutter界面切換實(shí)現(xiàn)點(diǎn)特效
- 01-10Android實(shí)現(xiàn)圓形漸變加載進(jìn)度條
- 01-10Emoji表情在Android JNI中的兼容性問題詳
隨機(jī)閱讀
- 04-02jquery與jsp,用jquery
- 01-10delphi制作wav文件的方法
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 08-05織夢dedecms什么時(shí)候用欄目交叉功能?
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-10C#中split用法實(shí)例總結(jié)