【发布时间】:2021-12-11 07:31:13
【问题描述】:
构建成功,但导航控制器不起作用。当我点击底部导航项时,对应的fragment不会覆盖宿主fragment。
MainActivity
class MainActivity : AppCompatActivity(){
private lateinit var binding: ActivityMainBinding
private lateinit var appBarConfiguration: AppBarConfiguration
private lateinit var navController: NavController
private var isInitial = true
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
initView()
}
private fun initView(){
navController = findNavController(R.id.nav_host_main)
binding.bottomNavMain.setupWithNavController(navController)
binding.leftNavMain.setupWithNavController(navController)
appBarConfiguration = AppBarConfiguration(setOf(R.id.dish_Fragment, R.id.cart_Fragment, R.id.home_Fragment, R.id.order_Fragment, R.id.friend_Fragment), binding.drawerLayoutMain)
setupActionBarWithNavController(navController, appBarConfiguration)
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
return item.onNavDestinationSelected(navController) ||
super.onOptionsItemSelected(item)
}
override fun onSupportNavigateUp(): Boolean {
return findNavController(R.id.nav_host_main).navigateUp(appBarConfiguration)
}
}
Main xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.drawerlayout.widget.DrawerLayout
android:id="@+id/drawer_layout_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/container_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_nav_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:menu="@menu/bottom_navigation" />
<fragment
android:id="@+id/nav_host_main"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/mobile_navigation" />
</androidx.constraintlayout.widget.ConstraintLayout>
<com.google.android.material.navigation.NavigationView
android:id="@+id/left_nav_main"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:menu="@menu/activity_main_drawer" />
</androidx.drawerlayout.widget.DrawerLayout>
</layout>
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/homeFragment">
<fragment
android:id="@+id/nav_home"
android:name="com.example.shereats.view.ui.home.MHomeFragment"
android:label="@string/menu_home"
tools:layout="@layout/fragment_home" />
<fragment
android:id="@+id/nav_gallery"
android:name="com.example.shereats.view.ui.gallery.GalleryFragment"
android:label="@string/menu_gallery"
tools:layout="@layout/fragment_gallery" />
<fragment
android:id="@+id/nav_slideshow"
android:name="com.example.shereats.view.ui.slideshow.SlideshowFragment"
android:label="@string/menu_slideshow"
tools:layout="@layout/fragment_slideshow" />
<fragment
android:id="@+id/dish_Fragment"
android:name="com.example.shereats.view.fragment.DishFragment"
android:label="dish_fragment"
tools:layout="@layout/dish_fragment" />
<fragment
android:id="@+id/cart_Fragment"
android:name="com.example.shereats.view.fragment.CartFragment"
android:label="cart_fragment"
tools:layout="@layout/cart_fragment" />
<fragment
android:id="@+id/home_Fragment"
android:name="com.example.shereats.view.fragment.HomeFragment"
android:label="home_fragment"
tools:layout="@layout/home_fragment" />
<fragment
android:id="@+id/order_Fragment"
android:name="com.example.shereats.view.fragment.OrderFragment"
android:label="order_fragment"
tools:layout="@layout/order_fragment" />
<fragment
android:id="@+id/friend_Fragment"
android:name="com.example.shereats.view.fragment.FriendFragment"
android:label="friend_fragment"
tools:layout="@layout/friend_fragment" />
</navigation>
【问题讨论】:
标签: android android-jetpack drawerlayout android-jetpack-navigation