Android實(shí)現(xiàn)底部導(dǎo)航欄的主界面
在主流app中,應(yīng)用的主界面都是底部含有多個(gè)標(biāo)簽的導(dǎo)航欄,點(diǎn)擊可以切換到相應(yīng)的界面,如圖:

接下來(lái)將描述下其實(shí)現(xiàn)過(guò)程。
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="首頁(yè)" /> <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)簽對(duì)應(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)擊或者切換回來(lái)都不會(huì)再加載以耗費(fèi)資源,這里常見(jiàn)的處理方式有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();
}
我們來(lái)逐行分析代碼
首先開啟一個(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,自動(dòng)執(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對(duì)象,當(dāng)點(diǎn)擊某個(gè)標(biāo)簽時(shí),如果當(dāng)前fragment對(duì)象為空,就生成一個(gè)對(duì)應(yīng)fragment的對(duì)象實(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;
}
生成對(duì)象后,我們就可以進(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做比較,這樣用來(lái)判斷底部導(dǎo)航欄是否點(diǎn)擊進(jìn)行了切換,首先當(dāng)應(yīng)用打開時(shí),因?yàn)槲覀兦懊嬲{(diào)用了第一個(gè)標(biāo)簽自動(dòng)點(diǎn)擊方法。
home.performClick();
看流程,因?yàn)榇藭r(shí)mContent還為null,所以走這段代碼
if (mContent == null) {
transaction.add(layout.getId(), fragment).commit();
mContent = fragment;
}
此時(shí)fragment即為HomeFragment對(duì)象,頁(yè)面將顯示HomeFragment,并將該對(duì)象賦給mContent。
此時(shí)HomeFragment生命周期如下,從Attach()走到onResume(),一切正常。

接下來(lái),點(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)檫€沒(méi)被加載過(guò),所以先走
transaction.hide(mContent).add(layout.getId(), fragment).commitAllowingStateLoss();
即隱藏掉mContent即HomeFragment,在將GameFragment加載顯示出來(lái)。
這時(shí)看GameFragment的生命周期,一切正常。

這時(shí)我們?cè)冱c(diǎn)擊回HomeFragment,此時(shí)fragment為HomeFragment,mContent為GameFragment,同時(shí)HomeFragment因?yàn)楸籥dd過(guò),所以走
transaction.hide(mContent).show(fragment).commitAllowingStateLoss();
這條語(yǔ)句指隱藏fragment同時(shí)不必加載add已經(jīng)加載過(guò)的fragment,直接show就可以,commitAllowingStateLoss方法與commit方法作用類似,更適用這種頻繁切換頁(yè)面下的提交工作,避免crash。
同時(shí)打開日志,發(fā)現(xiàn)HomeFragment并沒(méi)有被銷毀重載,這樣就達(dá)到了我們不想切換頻繁加載的目的。
至此全部完成,Demo附上!
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android實(shí)現(xiàn)EditText輸入金額
EditText是Android中一個(gè)非常實(shí)用的控件,有很多InputType,可以來(lái)達(dá)到不同的輸入效果,下面通過(guò)實(shí)例代碼給大家解析android實(shí)現(xiàn)edittext輸入金額,需要的朋友參考下吧2016-12-12
Android開發(fā)實(shí)現(xiàn)瀏覽器全屏顯示功能
這篇文章主要介紹了Android開發(fā)實(shí)現(xiàn)瀏覽器全屏顯示功能,涉及Android布局修改及相關(guān)屬性動(dòng)態(tài)設(shè)置操作技巧,需要的朋友可以參考下2017-09-09
Android6.0 storage目錄sd卡存儲(chǔ)的路徑創(chuàng)建詳解
這篇文章主要介紹了Android6.0 storage目錄sd卡存儲(chǔ)的路徑創(chuàng)建的相關(guān)資料,需要的朋友可以參考下2017-01-01
Android Studio 導(dǎo)入開源項(xiàng)目的正確姿勢(shì)及注意事項(xiàng)
這篇文章主要介紹了Android Studio 導(dǎo)入開源項(xiàng)目的正確姿勢(shì)及注意事項(xiàng),需要的朋友參考下吧2018-03-03
Android的HTTP擴(kuò)展包OkHttp中的緩存功能使用方法解析
OkHttp(GitHub主頁(yè)https://github.com/square/okhttp)是一款高人氣的第三方Android網(wǎng)絡(luò)編程包,這里我們來(lái)看一下Android的HTTP擴(kuò)展包OkHttp中的緩存功能使用方法解析:2016-07-07
Android開發(fā)筆記XML數(shù)據(jù)解析方法及優(yōu)缺點(diǎn)
XML數(shù)據(jù)是一種常見(jiàn)的數(shù)據(jù)格式,Android開發(fā)中需要對(duì)其進(jìn)行解析。常用的XML解析方式有DOM、SAX、Pull和Json等,每種方式都有其優(yōu)缺點(diǎn)。開發(fā)者可以根據(jù)具體需求選擇合適的解析方式,提高數(shù)據(jù)解析效率和性能2023-05-05

