gpt4 book ai didi

android - 底部导航 setOnItemSelectedListener 返回 super

转载 作者:行者123 更新时间:2023-12-04 23:58:45 27 4
gpt4 key购买 nike

我的 botton navation 设置如下,它工作正常。

navController = findNavController(R.id.nav_host_fragment)
appBarConfiguration = AppBarConfiguration(setOf(R.id.nav_1, R.id.nav_2))
setupActionBarWithNavController(navController, appBarConfiguration)
binding.bottomNavView.setupWithNavController(navController)
现在,如果当前 fragment 是第二个 fragment 并且其中设置了一些标志,我想防止切换 fragment 。
我试过使用 bottomNavView.setOnItemSelectedListener但它破坏了整个导航 Controller 机制,需要自己进行 fragment 和标题切换。
是否有任何其他方法可以覆盖特定项目的导航 Controller 行为,并让它像往常一样处理其余部分,例如 super.onItemSelected() ?

最佳答案

根据 the setupWithNavController() documentation :

This will call onNavDestinationSelected when a menu item is selected.


因此,如果您想从自己的监听器中执行默认行为,您可以简单地调用该方法。
// Call setupWithNavController to get the automatic
// selection of the correct bottom nav item
// based on the NavController's state
binding.bottomNavView.setupWithNavController(navController)
// Then override the OnItemSelectedListener
// with your own with your custom logic
binding.bottomNavView.setOnItemSelectedListener { item ->
if (navController.currentDestination?.id == R.id.fragment_id && !yourConditionIsMet) {
// Return false to not allow navigating to the tab
false
} else {
// Do the default behavior
onNavDestinationSelected(
item,
navController
)
}
}

关于android - 底部导航 setOnItemSelectedListener 返回 super,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70599960/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com