android開發中BottomNavigationBar使用例子

來源:互聯網
上載者:User

這次寫個新的底部欄實現方式,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即可,以上就是全部內容,用起來效果不錯,推薦使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.