1. 相关库的依赖
implementation "com.google.android.material:material:1.4.0"//MD库
//navigation 库的依赖
implementation "androidx.navigation:navigation-fragment:2.1.0"
implementation "androidx.navigation:navigation-ui:2.1.0"
2. xml 下的文件编写底部导航栏和navigation的配置
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/nav_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/bottom_nav_menu" />
<!--android:name="androidx.navigation.fragment.NavHostFragment" 这个是规定 -->
<!--app:defaultNavHost="true" 这个是把控制交给navhost控制 -->
<!-- app:navGraph="@navigation/graph_navigation" 这个是navigation 下fragment 的各个配置及入口 -->
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="?attr/actionBarSize"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="@id/nav_view"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0"
app:navGraph="@navigation/mobile_navigation" />
2.1 BottomNavigationView的menu 的配置
在res下新建menu 文件夹 新建菜单文件 bottom_nav_menu.xml
注意:该文件中的item id 要和后面 的 app:navGraph="@navigation/graph_navigation"下的id一致
<?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_project"
android:icon="@drawable/ic_dashboard_black_24dp"
android:title="@string/title_project" />
<item
android:id="@+id/navigation_dashboard"
android:icon="@drawable/ic_launcher_foreground"
android:title="@string/title_dashboard" />
</menu>
2.2 fragment的navGraph的配置
在res下新建navigation文件夹 新建菜单文件 graph_navigation.xml
<?xml version="1.0" encoding="utf-8"?>
<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="com.tjf.home.HomeFragment"
android:label="@string/title_home"
tools:layout="" />
<fragment
android:id="@+id/navigation_project"
android:name="com.tjf.project.ProjectFragment"
android:label="@string/title_project"
tools:layout="" />
<fragment
android:id="@+id/navigation_dashboard"
android:name="com.tjf.wan_navigation.NavFragment"
android:label="@string/title_dashboard"
tools:layout="" />
</navigation>
3. 在页面中的使用
//底部布局的获取及导航控制器的设置
val navView = findViewById<BottomNavigationView>(R.id.nav_view)
val navController: NavController =
Navigation.findNavController(this, R.id.nav_host_fragment)
//进行关联
NavigationUI.setupWithNavController(navView, navController)
//避免选中后再次点击进行重复创建的监听过滤
navView.setOnNavigationItemSelectedListener {
// 避免再次点击重复创建
if (it.isChecked) {
return@setOnNavigationItemSelectedListener true
}
return@setOnNavigationItemSelectedListener NavigationUI.onNavDestinationSelected(
it, navController
)
}