最近学习jetpack Navigation导航组件时,用BottomNavigationView与Navigation配合完成主页单activity与多fragment导航功能,一切准备就绪,却发现通过BottomNavigationView扩展方法来关联BottomNavigationView与Navigation时不生效,activity代码如下:
package com.dylan.jetpackusage
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.findNavController
import androidx.navigation.ui.setupWithNavController
import kotlinx.android.synthetic.main.activity_navigation.*
/**
* Created by Dylan on 2021/7/2.
* E-mail: andev.hoo@gmail.com
* desc:
*/
class NavigationActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_navigation)
supportActionBar?.hide()
val navController = findNavController(R.id.fragmentContainerFcv)
// supportFragmentManager.findFragmentById(R.id.fragmentContainerFcv)!!.findNavController()
/* bottomNavigationViewBnv.setOnNavigationItemSelectedListener { menuItem ->
Log.e("NavigationActivity", "menu id: ${menuItem.itemId}")
Log.e("NavigationActivity", "group id: ${menuItem.groupId}")
when (menuItem.itemId) {
R.id.nav_home -> navController.navigate(R.id.nav_home)
R.id.nav_message -> navController.navigate(R.id.nav_message)
R.id.nav_mine -> navController.navigate(R.id.nav_mine)
}
return@setOnNavigationItemSelectedListener true
}*/
bottomNavigationViewBnv.setupWithNavController(navController)
}
}
activity的布局文件activity_navigation代码如下:
<?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:layout_height="match_parent"
android:orientation="vertical"
tools:context=".NavigationActivity">
<fragment
android:id="@+id/fragmentContainerFcv"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#f9f9f9"
app:navGraph="@navigation/nav_graph" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#DDD9D9" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottomNavigationViewBnv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/windowBackground"
app:menu="@menu/bottom_nav_menu" />
</LinearLayout>
导航图文件nav_graph如下:
<?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/nav_graph"
app:startDestination="@id/nav_home_frag">
<fragment
android:id="@+id/nav_home_frag"
android:name="com.dylan.jetpackusage.fragment.HomeFragment"
android:label="@string/main_page"
tools:layout="@layout/fragment_home" />
<fragment
android:id="@+id/nav_message_frag"
android:name="com.dylan.jetpackusage.fragment.MsgFragment"
android:label="@string/msg"
tools:layout="@layout/fragment_msg" />
<fragment
android:id="@+id/nav_mine_frag"
android:name="com.dylan.jetpackusage.fragment.MineFragment"
android:label="@string/mine"
tools:layout="@layout/fragment_mine" />
</navigation>
选项菜单bottom_nav_menu如下:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/nav_home"
android:icon="@drawable/ic_home_black_24dp"
android:title="@string/main_page" />
<item
android:id="@+id/nav_message"
android:icon="@drawable/ic_notifications_black_24dp"
android:title="@string/msg" />
<item
android:id="@+id/nav_mine"
android:icon="@drawable/ic_dashboard_black_24dp"
android:title="@string/mine" />
</menu>
运行效果如下:
运行之后发现点击底部导航按钮,没有切换到对应fragment,最后排查到,是因为nav_graph导航图中的fragment的id没有与bottom_nav_menu选项菜单中item的id一一对应,将其一一对应即可,这里将nav_graph中fragment的id进行修改,与bottom_nav_menu的保持一致,修改后的代码如下:
<?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/nav_graph"
app:startDestination="@id/nav_home">
<fragment
android:id="@+id/nav_home"
android:name="com.dylan.jetpackusage.fragment.HomeFragment"
android:label="@string/main_page"
tools:layout="@layout/fragment_home" />
<fragment
android:id="@+id/nav_message"
android:name="com.dylan.jetpackusage.fragment.MsgFragment"
android:label="@string/msg"
tools:layout="@layout/fragment_msg" />
<fragment
android:id="@+id/nav_mine"
android:name="com.dylan.jetpackusage.fragment.MineFragment"
android:label="@string/mine"
tools:layout="@layout/fragment_mine" />
</navigation>
修改之后,就可以将BottomNavigationView与Navigation进行关联,点击底部选项卡自动切换到对应fragment了。
小结
BottomNavigationView的menu的item的id需要与Navigation的对应的fragment的id保持一致,才能通过*bottomNavigationViewBnv.setupWithNavController(navController)*进行自动关联。