這次寫個新的底部欄實現方式,BottomNavigationBar,貌似上個月Google發布的,使用起來也很方便下面簡單介紹下使用方式,
1、compile ‘com.ashokvarma.android:bottom-navigation-bar:0.9.5’ 在gradle中加入這句
2、布局檔案
<pre><?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.chenxu.mybottomdemo.MainActivity">
<LinearLayout
android:id="@+id/ll_root"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/bottom_bar"
/>
<com.ashokvarma.bottomnavigation.BottomNavigationBar
android:id="@+id/bottom_bar"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true" />
</RelativeLayout></pre>
3、Java代碼
<pre>package com.example.chenxu.mybottomdemo;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
public class MainActivity extends AppCompatActivity implements BottomNavigationBar.OnTabSelectedListener {
private BottomNavigationBar mBottomBar;
private FragmentOne fragmentOne;
private FragmentTwo fragmentTwo;
private FragmentThree fragmentThree;
private FragmentFore fragmentFore;
private FragmentFive fragmentFive;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
assignViews();
}
/**
* 添加頁面
*/
private void assignViews() {
mBottomBar = (BottomNavigationBar) findViewById(R.id.bottom_bar);
mBottomBar.addItem(new BottomNavigationItem(R.mipmap.ic_launcher, "首頁"))
.addItem(new BottomNavigationItem(R.mipmap.ic_launcher, "商品"))
.addItem(new BottomNavigationItem(R.mipmap.ic_launcher, "分類"))
.addItem(new BottomNavigationItem(R.mipmap.ic_launcher, "購物車"))
.addItem(new BottomNavigationItem(R.mipmap.ic_launcher, "我的"))
.initialise();
mBottomBar.setTabSelectedListener(this);//設定監聽
setDefaultFragment();//設定預設選項
}
private void setDefaultFragment() {
FragmentManager fragmentManager=getSupportFragmentManager();
FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
hideFragments(fragmentTransaction);
fragmentOne=new FragmentOne();
fragmentTransaction.add(R.id.ll_root,fragmentOne);
fragmentTransaction.commit();
}
/**
* 隱藏fragment
* @param transaction
*/
private void hideFragments(FragmentTransaction transaction) {
if (fragmentOne != null) {
transaction.hide(fragmentOne);
}
if (fragmentTwo != null) {
transaction.hide(fragmentTwo);
}
if (fragmentThree != null) {
transaction.hide(fragmentThree);
}
if (fragmentFore != null) {
transaction.hide(fragmentFore);
}
if (fragmentFive != null) {
transaction.hide(fragmentFive);
}
}
@Override
public void onTabSelected(int position) {
FragmentManager fragmentManager=getSupportFragmentManager();
FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
hideFragments(fragmentTransaction);
switch (position){
case 0:
if (fragmentOne==null){
fragmentOne=new FragmentOne();
fragmentTransaction.add(R.id.ll_root,fragmentOne);
}else {
fragmentTransaction.show(fragmentOne);
}
break;
case 1:
if (fragmentTwo==null){
fragmentTwo=new FragmentTwo();
fragmentTransaction.add(R.id.ll_root,fragmentTwo);
}else {
fragmentTransaction.show(fragmentTwo);
}
break;
case 2:
if (fragmentThree==null){
fragmentThree=new FragmentThree();
fragmentTransaction.add(R.id.ll_root,fragmentThree);
}else {
fragmentTransaction.show(fragmentThree);
}
break;
case 3:
if (fragmentFore==null){
fragmentFore=new FragmentFore();
fragmentTransaction.add(R.id.ll_root,fragmentFore);
}else {
fragmentTransaction.show(fragmentFore);
}
break;
case 4:
if (fragmentFive==null){
fragmentFive=new FragmentFive();
fragmentTransaction.add(R.id.ll_root,fragmentFive);
}else {
fragmentTransaction.show(fragmentFive);
}
break;
}
fragmentTransaction.commit();
}
@Override
public void onTabUnselected(int position) {
}
@Override
public void onTabReselected(int position) {
}
}</pre>
註:每個底部按鈕對應一個fragment即可,以上就是全部內容,用起來效果不錯,推薦使用