如何使用 AppBarConfiguration 设置 NavigationView?
How to set NavigationView with AppBarConfiguration?
我是导航新手,我在导航组件中的起始目的地与我的首选目的地不同,所以我使用 AppBarConfiguration
来使用自定义首选目的地,但是当我想将 NavigationView 设置为 navController
使用 NavigationUI.setupWithNavController(binding.navView, navController)
设置带有抽屉项目的导航组件,但是当我在导航抽屉中按下一个项目时,应用程序无法识别我的自定义顶级目的地,导航到不同的片段并按下后退按钮导航回顶级目的地。它向我显示了起始目的地,但我想查看自定义的首选目的地
这是他们的主要 activity 代码:
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.databinding.DataBindingUtil
import androidx.drawerlayout.widget.DrawerLayout
import androidx.navigation.NavController
import androidx.navigation.NavDestination
import androidx.navigation.findNavController
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.NavigationUI
import com.example.android.pass.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
private lateinit var drawerLayout: DrawerLayout
private lateinit var appBarConfiguration: AppBarConfiguration
private lateinit var binding: ActivityMainBinding
private lateinit var navController: NavController
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setTheme(R.style.AppTheme)
binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
val toolbar= binding.toolbar
drawerLayout = binding.drawerLayout
navController = this.findNavController(R.id.myNavHostFragment)
setSupportActionBar(toolbar)
navController
.addOnDestinationChangedListener { nc: NavController, nd: NavDestination, args: Bundle? ->
if (nd.id == R.id.accountListFragment) {
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED)
} else {
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED)
}
}
val topLevelDestinations = setOf(R.id.accountListFragment)
appBarConfiguration = AppBarConfiguration.Builder(topLevelDestinations)
.setDrawerLayout(drawerLayout)
.build()
NavigationUI.setupWithNavController(binding.navView, navController)
NavigationUI.setupWithNavController(toolbar,navController,appBarConfiguration)
}
override fun onSupportNavigateUp(): Boolean {
return NavigationUI.navigateUp(navController,appBarConfiguration)
}
}
这是我的抽屉菜单,其中有两个项目的 ID 与我要导航的片段相同:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<item
android:id="@+id/aboutFragment"
android:icon="@drawable/question"
android:title="About" />
<item
android:id="@+id/settingsFragment"
android:icon="@drawable/settings"
android:title="settings" />
</menu>
使用这个:
AppBarConfiguration appBarConfiguration =
new AppBarConfiguration.Builder(navController.getGraph()).build();
而不是:
appBarConfiguration = AppBarConfiguration.Builder(topLevelDestinations)
我找到了答案,而不是使用以下代码将 navigationView
设置为 navController
:
NavigationUI.setupWithNavController(binding.navView, navController)
我在主要 activity:
中使用了以下代码
binding.navView.setNavigationItemSelectedListener(this)
override fun onNavigationItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.about_item -> navController.navigate(R.id.aboutFramgent)
R.id.settings_item -> navController.navigate(R.id.settingsFramgent)
}
return true
}
我是导航新手,我在导航组件中的起始目的地与我的首选目的地不同,所以我使用 AppBarConfiguration
来使用自定义首选目的地,但是当我想将 NavigationView 设置为 navController
使用 NavigationUI.setupWithNavController(binding.navView, navController)
设置带有抽屉项目的导航组件,但是当我在导航抽屉中按下一个项目时,应用程序无法识别我的自定义顶级目的地,导航到不同的片段并按下后退按钮导航回顶级目的地。它向我显示了起始目的地,但我想查看自定义的首选目的地
这是他们的主要 activity 代码:
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.databinding.DataBindingUtil
import androidx.drawerlayout.widget.DrawerLayout
import androidx.navigation.NavController
import androidx.navigation.NavDestination
import androidx.navigation.findNavController
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.NavigationUI
import com.example.android.pass.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
private lateinit var drawerLayout: DrawerLayout
private lateinit var appBarConfiguration: AppBarConfiguration
private lateinit var binding: ActivityMainBinding
private lateinit var navController: NavController
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setTheme(R.style.AppTheme)
binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
val toolbar= binding.toolbar
drawerLayout = binding.drawerLayout
navController = this.findNavController(R.id.myNavHostFragment)
setSupportActionBar(toolbar)
navController
.addOnDestinationChangedListener { nc: NavController, nd: NavDestination, args: Bundle? ->
if (nd.id == R.id.accountListFragment) {
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED)
} else {
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED)
}
}
val topLevelDestinations = setOf(R.id.accountListFragment)
appBarConfiguration = AppBarConfiguration.Builder(topLevelDestinations)
.setDrawerLayout(drawerLayout)
.build()
NavigationUI.setupWithNavController(binding.navView, navController)
NavigationUI.setupWithNavController(toolbar,navController,appBarConfiguration)
}
override fun onSupportNavigateUp(): Boolean {
return NavigationUI.navigateUp(navController,appBarConfiguration)
}
}
这是我的抽屉菜单,其中有两个项目的 ID 与我要导航的片段相同:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<item
android:id="@+id/aboutFragment"
android:icon="@drawable/question"
android:title="About" />
<item
android:id="@+id/settingsFragment"
android:icon="@drawable/settings"
android:title="settings" />
</menu>
使用这个:
AppBarConfiguration appBarConfiguration =
new AppBarConfiguration.Builder(navController.getGraph()).build();
而不是:
appBarConfiguration = AppBarConfiguration.Builder(topLevelDestinations)
我找到了答案,而不是使用以下代码将 navigationView
设置为 navController
:
NavigationUI.setupWithNavController(binding.navView, navController)
我在主要 activity:
binding.navView.setNavigationItemSelectedListener(this)
override fun onNavigationItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.about_item -> navController.navigate(R.id.aboutFramgent)
R.id.settings_item -> navController.navigate(R.id.settingsFramgent)
}
return true
}