Android使用BottomNavigationView與NavController來實現底部導航欄

1.主佈局

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="?attr/actionBarSize">

    <!--底部導航欄-->
    <android.support.design.widget.BottomNavigationView
        android:id="@+id/nav_view"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="0dp"
        android:layout_marginEnd="0dp"
        android:background="?android:attr/windowBackground"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/bottom_nav_menu" />
    <!--menu指定了導航欄菜單-->

    <fragment
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toTopOf="@id/nav_view"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/mobile_navigation" />
    <!--navGraph指定了點擊菜單後的導航規則-->

</android.support.constraint.ConstraintLayout>

BottomNavigationView:底部導航視圖,通過menu指定導航菜單,即導航ID、圖標、標題

fragment:導航切換的Fragment佔位符,通過navGraph指定了導航規則,即ID與Fragment實現類的映射

 

2.導航菜單佈局

<?xml version="1.0" encoding="utf-8"?>
<!--底部菜單配置-->
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/navigation_home"
        android:icon="@drawable/ic_home_black_24dp"
        android:title="@string/title_home" />

    <item
        android:id="@+id/navigation_dashboard"
        android:icon="@drawable/ic_dashboard_black_24dp"
        android:title="@string/title_dashboard" />

    <item
        android:id="@+id/navigation_notifications"
        android:icon="@drawable/ic_notifications_black_24dp"
        android:title="@string/title_notifications" />

</menu>

可以看到每個導航菜單包含了導航的ID、圖標、標題

 

3.導航規則

<?xml version="1.0" encoding="utf-8"?>

<!--導航hi哦則,包含了導航ID與Fragment的映射-->
<navigation 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:id="@+id/mobile_navigation"
    app:startDestination="@+id/navigation_home">
    <!--開始時的導航目的地-->
    
    <fragment
        android:id="@+id/navigation_home"
        android:name="my.first.app.ui.home.HomeFragment"
        android:label="@string/title_home"
        tools:layout="@layout/fragment_home" />

    <fragment
        android:id="@+id/navigation_dashboard"
        android:name="my.first.app.ui.dashboard.DashboardFragment"
        android:label="@string/title_dashboard"
        tools:layout="@layout/fragment_dashboard" />

    <fragment
        android:id="@+id/navigation_notifications"
        android:name="my.first.app.ui.notifications.NotificationsFragment"
        android:label="@string/title_notifications"
        tools:layout="@layout/fragment_notifications" />
</navigation>

每個導航規則都包含了導航ID與一個Fragment類的映射,此處的ID與菜單的ID一致

 

4.主Activity

/**
 * 主頁面,用於分類查看功能
 */
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        /* 主頁佈局 */
        setContentView(R.layout.activity_main);
        /* 獲取底部導航欄視圖 */
        BottomNavigationView navView = findViewById(R.id.nav_view);
        /* AppBar配置 */
        AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications).build();
        /* 從Fragment獲取導航控制器,navGraph定義了導航規則 */
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        /* 爲AppBar設置導航控制器,監聽導航改變事件,修改標題 */
        NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
        /* 爲視圖設置導航控制器,即監聽視圖的點擊事件 */
        NavigationUI.setupWithNavController(navView, navController);
    }

}

NavController爲導航控制器,裏面包含了導航規則,是導航的中樞

NavigationUI.setupActionBarWithNavController,設置NavController導航改變的監聽事件,修改頁面標題欄

NavigationUI.setupWithNavController,設置觸發導航改變的事件,當底部導航欄被點擊時觸發導航

 

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