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

接下來將描述下其實現(xiàn)過程。
1.首先是分析界面,底部導(dǎo)航欄我們可以用一個占滿屏幕寬度、包裹著數(shù)個標(biāo)簽TextView、方向為橫向horizontal的線性布局LinearLayout。上方則是一個占滿剩余空間的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.四個標(biāo)簽對應(yīng)四個Fragment,我們新建四個Fragment,布局放個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點擊事件放在后面處理
3.初始化四個fragment
我們初衷是讓fragment加載一次后,重復(fù)點擊或者切換回來都不會再加載以耗費資源,這里常見的處理方式有viewpager的懶加載和fragment的hide、show,這里我們講解后者的實現(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();
}
我們來逐行分析代碼
首先開啟一個事務(wù)
FragmentTransaction transaction= getSupportFragmentManager().beginTransaction();
進行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的點擊事件,即默認(rèn)顯示HomeFragment
homeFragment= null; gameFragment= null; videoFragment= null; mineFragment= null; home.performClick();
4.回到四個底部標(biāo)簽的點擊事件,我們執(zhí)行自定義的switchContent方法,將當(dāng)前點擊標(biāo)簽的view作為參數(shù)傳進去
@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方法
新建一個fragment對象,當(dāng)點擊某個標(biāo)簽時,如果當(dāng)前fragment對象為空,就生成一個對應(yīng)fragment的對象實例
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;
}
生成對象后,我們就可以進行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和上個碎片fragment做比較,這樣用來判斷底部導(dǎo)航欄是否點擊進行了切換,首先當(dāng)應(yīng)用打開時,因為我們前面調(diào)用了第一個標(biāo)簽自動點擊方法。
home.performClick();
看流程,因為此時mContent還為null,所以走這段代碼
if (mContent == null) {
transaction.add(layout.getId(), fragment).commit();
mContent = fragment;
}
此時fragment即為HomeFragment對象,頁面將顯示HomeFragment,并將該對象賦給mContent。
此時HomeFragment生命周期如下,從Attach()走到onResume(),一切正常。

接下來,點擊第二個標(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因為還沒被加載過,所以先走
transaction.hide(mContent).add(layout.getId(), fragment).commitAllowingStateLoss();
即隱藏掉mContent即HomeFragment,在將GameFragment加載顯示出來。
這時看GameFragment的生命周期,一切正常。

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

