BottomNavigationView 實現底部導航欄


一、先導入依賴
// BottomNavigationView
implementation 'com.android.support:design:27.1.1'

二、(1)、在res -->創建menu 文件夾 --> 在寫 navigation 這個xml文件 ,裏面是你創建的底部按鈕的名稱和圖片
如圖:

(2)、菜單裏面的代碼

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/jingxuan"
        android:icon="@drawable/found"
        android:title="精選" />
    <item
        android:id="@+id/zhuanti"
        android:icon="@drawable/special"
        android:title="專題" />
    <item
        android:id="@+id/faxian"
        android:icon="@drawable/fancy"
        android:title="發現" />
    <item
        android:id="@+id/wode"
        android:icon="@drawable/my"
        android:title="我的" />
</menu>

三、在佈局裏面引用控件

<?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:orientation="vertical"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:textSize="20dp"
        android:gravity="center"
        android:background="#f89"
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="標題"/>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:id="@+id/fram_layout"></FrameLayout>

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/bottomBar"
        app:menu="@menu/navigation"
       android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>

四、在MainActivity裏面加載fragment -->主要代碼

package com.xwj.lenovo.xuwenjuan0514;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.xwj.lenovo.xuwenjuan0514.view.fragment.Fragment_faxian;
import com.xwj.lenovo.xuwenjuan0514.view.fragment.Fragment_jingxuan;
import com.xwj.lenovo.xuwenjuan0514.view.fragment.Fragment_wode;
import com.xwj.lenovo.xuwenjuan0514.view.fragment.Fragment_zhuanti;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.fram_layout)
    FrameLayout framLayout;
    @BindView(R.id.bottomBar)
    BottomNavigationView bottomBar;
    @BindView(R.id.text)
    TextView text;
    private FragmentManager supportFragmentManager;
    private Fragment_jingxuan fragment_jingxuan;
    private Fragment_wode fragment_wode;
    private Fragment_faxian fragment_faxian;
    private Fragment_zhuanti fragment_zhuanti;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        supportFragmentManager = getSupportFragmentManager();
        fragment_jingxuan = new Fragment_jingxuan();
        // 默認顯示第一個fragment
        supportFragmentManager.beginTransaction().add(R.id.fram_layout, fragment_jingxuan).commit();

        bottomBar.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {


            private FragmentTransaction fragmentTransaction;

            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                // 每次點擊時 隱藏所有的fragment
                hideAllFragments();
                // 開啓一個事務
                fragmentTransaction = supportFragmentManager.beginTransaction();
                // 根據點擊的按鈕 顯示對應的fragment

                switch (item.getItemId()) {
                    case R.id.jingxuan:
                        if (fragment_jingxuan == null) {
                            fragment_jingxuan = new Fragment_jingxuan();
                            // 如果這個碎片是空的 就把這個碎片添加到幀佈局中進行顯示
                            fragmentTransaction.add(R.id.fram_layout, fragment_jingxuan);
                            text.setText("精選");
                        } else {
                            // 如果這個碎片不是空的 直接顯示這個碎片
                            fragmentTransaction.show(fragment_jingxuan);
                            text.setText("精選");
                        }
                        fragmentTransaction.commit();
                        return true;

                    case R.id.zhuanti:
                        if (fragment_zhuanti == null) {
                            fragment_zhuanti = new Fragment_zhuanti();
                            fragmentTransaction.add(R.id.fram_layout, fragment_zhuanti);
                            text.setText("專題");
                        }else {
                            // 如果這個碎片不是空的 直接顯示這個碎片
                            fragmentTransaction.show(fragment_zhuanti);
                            text.setText("專題");
                        }
                        fragmentTransaction.commit();
                        return true;

                    case R.id.faxian:
                        if (fragment_faxian == null) {
                            fragment_faxian = new Fragment_faxian();
                            fragmentTransaction.add(R.id.fram_layout, fragment_faxian);
                            text.setText("發現");
                        }else {
                            // 如果這個碎片不是空的 直接顯示這個碎片
                            fragmentTransaction.show(fragment_faxian);
                            text.setText("發現");
                        }
                        fragmentTransaction.commit();
                        return true;

                    case R.id.wode:
                        if (fragment_wode == null) {
                            fragment_wode = new Fragment_wode();
                            fragmentTransaction.add(R.id.fram_layout, fragment_wode);
                            text.setText("我的");
                        }else {
                            // 如果這個碎片不是空的 直接顯示這個碎片
                            fragmentTransaction.show(fragment_wode);
                            text.setText("我的");
                        }
                        fragmentTransaction.commit();
                        return true;
                }
                return false;
            }
        });
    }
    // 隱藏所有的fragment
    public void hideAllFragments() {
        // 開啓一個事務
        FragmentTransaction hideTran = supportFragmentManager.beginTransaction();
        // 隱藏fragment
        if (fragment_jingxuan != null) hideTran.hide(fragment_jingxuan);
        if (fragment_zhuanti != null) hideTran.hide(fragment_zhuanti);
        if (fragment_faxian != null) hideTran.hide(fragment_faxian);
        if (fragment_wode != null) hideTran.hide(fragment_wode);

        // 提交事務
        hideTran.commit();
    }
}

寫上你需要的fragment頁面,以上基本就完成了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章