生活随笔
收集整理的這篇文章主要介紹了
Fragment实现的底部导航
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
流行的應用的導航一般分為兩種,一種是底部導航,一種是側邊欄。
我所做的項目涉及到比較多的是底部導航,今天我就把項目中使用的一種實現方式分享一下。
主要實現思路是:在一個Activity里面底部添加四個按鍵,上邊通過切換不同的Fragment來實現。
首先在activity_main.xml
實現一個底部布局
[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="發現"?/>?? ?????????????</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>??
這里主要用到的知識點有:style的使用,Radiobutton的一些特殊屬性設置,權重,以及drawableTop的使用。
底部布局實現之后,就需要在MainActivity里面找到對應的控件做出相應的邏輯處理了。
首先實例化一個FragmentManager實例
然后初始化首頁的布局,這里為微信首頁面,通過調用initFragment();來實現,接著就是點擊的處理dealBottomButtonsClickEvent();
代碼如下:
[html]?view plaincopy
/**?? ?*?初始化首個Fragment?? ?*/?? private?void?initFragment()?{?? ????FragmentTransaction?ft?=?fMgr.beginTransaction();?? ????WeiXinFragment?weiXinFragment?=?new?WeiXinFragment();?? ????ft.add(R.id.fragmentRoot,?weiXinFragment,?"weiXinFragment");?? ????ft.addToBackStack("weiXinFragment");?? ????ft.commit();?????????? }?? /**?? ?*?處理底部點擊事件?? ?*/?? 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();?? ?? ????????}?? ????});??
點擊初始化的代碼我只截取了一個,這里用到的幾個知識點是:
1、Fragment的管理,添加,放入堆棧,提交
2、關于Fragment的Tag的使用,因為在初始化的時候設置了tag,所以在點擊時間里面,判斷如果當前的的Fragment顯示著,那么就直接return,都則彈出之前所有的Fragment保留WeiXinFragment。
3、關于Fragment堆棧的問題,添加的時候是把一new的Fragment添加到堆棧里,這里調用了fMgr.popBackStack();
最后就是攔截返回鍵的處理了。
[java]?view plaincopy
?? @Override?? public?void?onBackPressed()?{?? ????if(fMgr.findFragmentByTag("weiXinFragment")!=null?&&?fMgr.findFragmentByTag("weiXinFragment").isVisible())?{?? ????????MainActivity.this.finish();?? ????}?else?{?? ????????super.onBackPressed();?? ????}?? }??
邏輯是:如果當前的Fragment是WeiXinFragment則推出當前應用,否則調用父返回鍵,這樣可以保證別的Fragment切換到WeixinFragment
代碼里涉及到的別的就是com.walker.fragmentnavigation.fragment包里的四個頁面布局,都是繼承自Fragment。
需要說明的知識點有:
1、onCreateView里面返回一個View,這個每個Fragment里面都使用了對于的布局
2、布局里面的頂部title是復用的<include layout="@layout/top_title" />
3、在Fragment里面有個getView()的方法可以找到對應的布局控件,然后修改
((TextView)getView().findViewById(R.id.tvTop)).setText("通訊錄");
注:用到了騰訊的幾張圖片
這個例子到這里就結束了,代碼下載
總結
以上是生活随笔為你收集整理的Fragment实现的底部导航的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。