日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

Fragment实现的底部导航

發(fā)布時(shí)間:2025/6/15 编程问答 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Fragment实现的底部导航 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

流行的應(yīng)用的導(dǎo)航一般分為兩種,一種是底部導(dǎo)航,一種是側(cè)邊欄。

我所做的項(xiàng)目涉及到比較多的是底部導(dǎo)航,今天我就把項(xiàng)目中使用的一種實(shí)現(xiàn)方式分享一下。

主要實(shí)現(xiàn)思路是:在一個(gè)Activity里面底部添加四個(gè)按鍵,上邊通過(guò)切換不同的Fragment來(lái)實(shí)現(xiàn)。

首先在activity_main.xml

實(shí)現(xiàn)一個(gè)底部布局

[html]?view plaincopy
  • <RelativeLayout??
  • ?????android:id="@+id/aboveLayout"??
  • ?????android:layout_width="fill_parent"??
  • ?????android:layout_height="fill_parent"??
  • ?????android:orientation="vertical"?>??
  • ??
  • ?????<RelativeLayout??
  • ?????????android:layout_width="fill_parent"??
  • ?????????android:layout_height="fill_parent"??
  • ?????????android:background="#dbdbdb"??
  • ?????????android:orientation="vertical"?>??
  • ??
  • ?????????<LinearLayout??
  • ?????????????android:id="@+id/fragmentRoot"??
  • ?????????????android:layout_width="fill_parent"??
  • ?????????????android:layout_height="fill_parent"?/>??
  • ??
  • ?????????<LinearLayout??
  • ?????????????android:id="@+id/bottomList"??
  • ?????????????android:layout_width="fill_parent"??
  • ?????????????android:layout_height="wrap_content"??
  • ?????????????android:layout_alignParentBottom="true"??
  • ?????????????android:background="@android:color/black"??
  • ?????????????android:orientation="horizontal"?>??
  • ??
  • ?????????????<LinearLayout??
  • ?????????????????android:id="@+id/bottomItemCurrentBg1"??
  • ?????????????????style="@style/main_bottom_item"?>??
  • ??
  • ?????????????????<RadioButton??
  • ?????????????????????android:id="@+id/rbMain"??
  • ?????????????????????style="@style/footbar"??
  • ?????????????????????android:drawableTop="@drawable/tab_weixin_normal"??
  • ?????????????????????android:paddingTop="8dp"??
  • ?????????????????????android:text="微信"?/>??
  • ?????????????</LinearLayout>??
  • ??
  • ?????????????<LinearLayout??
  • ?????????????????android:id="@+id/bottomItemCurrentBg2"??
  • ?????????????????style="@style/main_bottom_item"?>??
  • ??
  • ?????????????????<RadioButton??
  • ?????????????????????android:id="@+id/rbCategory"??
  • ?????????????????????style="@style/footbar"??
  • ?????????????????????android:drawableTop="@drawable/tab_address_normal"??
  • ?????????????????????android:paddingTop="8dp"??
  • ?????????????????????android:text="通訊錄"?/>??
  • ?????????????</LinearLayout>??
  • ??
  • ??
  • ?????????????<LinearLayout??
  • ?????????????????android:id="@+id/bottomItemCurrentBg3"??
  • ?????????????????style="@style/main_bottom_item"?>??
  • ??
  • ?????????????????<RadioButton??
  • ?????????????????????android:id="@+id/rbFind"??
  • ?????????????????????style="@style/footbar"??
  • ?????????????????????android:drawableTop="@drawable/tab_find_frd_normal"??
  • ?????????????????????android:paddingTop="8dp"??
  • ?????????????????????android:text="發(fā)現(xiàn)"?/>??
  • ?????????????</LinearLayout>??
  • ??
  • ??
  • ?????????????<LinearLayout??
  • ?????????????????android:id="@+id/bottomItemCurrentBg4"??
  • ?????????????????style="@style/main_bottom_item"?>??
  • ??
  • ?????????????????<RadioButton??
  • ?????????????????????android:id="@+id/rbMe"??
  • ?????????????????????style="@style/footbar"??
  • ?????????????????????android:drawableTop="@drawable/tab_settings_normal"??
  • ?????????????????????android:paddingTop="8dp"??
  • ?????????????????????android:text="我"?/>??
  • ?????????????</LinearLayout>??
  • ?????????</LinearLayout>??
  • ?????</RelativeLayout>??
  • ??
  • ?</RelativeLayout>??

  • 這里主要用到的知識(shí)點(diǎn)有:style的使用,Radiobutton的一些特殊屬性設(shè)置,權(quán)重,以及drawableTop的使用。

    底部布局實(shí)現(xiàn)之后,就需要在MainActivity里面找到對(duì)應(yīng)的控件做出相應(yīng)的邏輯處理了。

    首先實(shí)例化一個(gè)FragmentManager實(shí)例

    然后初始化首頁(yè)的布局,這里為微信首頁(yè)面,通過(guò)調(diào)用initFragment();來(lái)實(shí)現(xiàn),接著就是點(diǎn)擊的處理dealBottomButtonsClickEvent();

    代碼如下:

    [html]?view plaincopy
  • /**??
  • ?*?初始化首個(gè)Fragment??
  • ?*/??
  • private?void?initFragment()?{??
  • ????FragmentTransaction?ft?=?fMgr.beginTransaction();??
  • ????WeiXinFragment?weiXinFragment?=?new?WeiXinFragment();??
  • ????ft.add(R.id.fragmentRoot,?weiXinFragment,?"weiXinFragment");??
  • ????ft.addToBackStack("weiXinFragment");??
  • ????ft.commit();??????????
  • }??
  • /**??
  • ?*?處理底部點(diǎn)擊事件??
  • ?*/??
  • private?void?dealBottomButtonsClickEvent()?{???
  • ????findViewById(R.id.rbWeiXin).setOnClickListener(new?OnClickListener()?{??
  • ??????????
  • ????????@Override??
  • ????????public?void?onClick(View?v)?{??
  • ????????????//?TODO?Auto-generated?method?stub??
  • ????????????if(fMgr.findFragmentByTag("weiXinFragment")!=null?&&?fMgr.findFragmentByTag("weiXinFragment").isVisible())?{??
  • ????????????????return;??
  • ????????????}??
  • ????????????popAllFragmentsExceptTheBottomOne();??
  • ??
  • ????????}??
  • ????});??

  • 點(diǎn)擊初始化的代碼我只截取了一個(gè),這里用到的幾個(gè)知識(shí)點(diǎn)是:

    1、Fragment的管理,添加,放入堆棧,提交

    2、關(guān)于Fragment的Tag的使用,因?yàn)樵诔跏蓟臅r(shí)候設(shè)置了tag,所以在點(diǎn)擊時(shí)間里面,判斷如果當(dāng)前的的Fragment顯示著,那么就直接return,都則彈出之前所有的Fragment保留WeiXinFragment。

    3、關(guān)于Fragment堆棧的問(wèn)題,添加的時(shí)候是把一new的Fragment添加到堆棧里,這里調(diào)用了fMgr.popBackStack();

    最后就是攔截返回鍵的處理了。

    [java]?view plaincopy
  • //點(diǎn)擊返回按鈕??
  • @Override??
  • public?void?onBackPressed()?{??
  • ????if(fMgr.findFragmentByTag("weiXinFragment")!=null?&&?fMgr.findFragmentByTag("weiXinFragment").isVisible())?{??
  • ????????MainActivity.this.finish();??
  • ????}?else?{??
  • ????????super.onBackPressed();??
  • ????}??
  • }??

  • 邏輯是:如果當(dāng)前的Fragment是WeiXinFragment則推出當(dāng)前應(yīng)用,否則調(diào)用父返回鍵,這樣可以保證別的Fragment切換到WeixinFragment

    代碼里涉及到的別的就是com.walker.fragmentnavigation.fragment包里的四個(gè)頁(yè)面布局,都是繼承自Fragment。

    需要說(shuō)明的知識(shí)點(diǎn)有:

    1、onCreateView里面返回一個(gè)View,這個(gè)每個(gè)Fragment里面都使用了對(duì)于的布局

    2、布局里面的頂部title是復(fù)用的<include layout="@layout/top_title" />

    3、在Fragment里面有個(gè)getView()的方法可以找到對(duì)應(yīng)的布局控件,然后修改

    ((TextView)getView().findViewById(R.id.tvTop)).setText("通訊錄");


    注:用到了騰訊的幾張圖片

    這個(gè)例子到這里就結(jié)束了,代碼下載


    總結(jié)

    以上是生活随笔為你收集整理的Fragment实现的底部导航的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

    如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。