【已解决】 Navigation和BottomNavigationView结合点击底部菜单无法切换fragment
// 主activity
class BottomNavigationActivity : AppCompatActivity() {
private lateinit var binding: ActivityBottomNavigationBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_bottom_navigation)
// 初始化 binding
binding = ActivityBottomNavigationBinding.inflate(layoutInflater)
// val control = findNavController(R.id.nav_host_fragment)
// NavigationUI.setupWithNavController(binding.bottomNavigation, control)
val navHostFragment =
supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
binding.bottomNavigation.setupWithNavController(navHostFragment.navController)
Log.d("BottomNavigationActivity", "navHostFragment: ${navHostFragment.navController}")
}
}
// 页面布局
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".demo11.BottomNavigationActivity">
<!-- Fragment 容器:使用 FragmentContainerView -->
<androidx.fragment.app.FragmentContainerView
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/bottom_navigation"
app:defaultNavHost="true"
app:navGraph="@navigation/mobile_navigation" />
<!-- 底部导航栏 -->
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
app:labelVisibilityMode="labeled"
app:layout_constraintBottom_toBottomOf="parent"
app:menu="@menu/bottom_nav_menu" />
</androidx.constraintlayout.widget.ConstraintLayout>
// nav_Graph.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/nav_artist">
<fragment
android:id="@+id/nav_artist"
android:name="com.example.myapplication.demo11.ArtistFragment"
android:label="fragment_artist"
tools:layout="@layout/fragment_artist" />
<fragment
android:id="@+id/nav_album"
android:name="com.example.myapplication.demo11.AlbumFragment"
android:label="fragment_album"
tools:layout="@layout/fragment_album" />
<fragment
android:id="@+id/nav_pause"
android:name="com.example.myapplication.demo11.PauseFragment"
android:label="fragment_pause"
tools:layout="@layout/fragment_pause" />
</navigation>
// menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/nav_artist"
android:icon="@drawable/icon_artist"
android:title="艺术家" />
<item
android:id="@+id/nav_album"
android:icon="@drawable/icon_album"
android:title="专辑" />
<item
android:id="@+id/nav_pause"
android:icon="@drawable/icon_pause"
android:title="暂停" />
</menu>
setContentView(R.layout.activity_bottom_navigation)
binding = ActivityBottomNavigationBinding.inflate(layoutInflater)
你这里是不是反了,应该是:
binding = ActivityBottomNavigationBinding.inflate(layoutInflater)
setContentView(binding.root)