在按钮上浏览底部navbar -jetpack组成
我的底部navigationbar有4个项目,当用户单击按钮时,我想导航到最后一个项目。
这是我的底部努力栏:
@Composable
fun BottomNavigationBar(navController: NavController) {
val items = listOf(
BottomNavigationItem.Home,
BottomNavigationItem.Explore,
BottomNavigationItem.Favorites,
BottomNavigationItem.Profile
)
val navBackStackEntry by navController.currentBackStackEntryAsState()
val currentRoute = navBackStackEntry?.destination?.route
BottomNavigation(
backgroundColor = colorResource(id = R.color.purple_700),
contentColor = Color.White
) {
Row(horizontalArrangement = Arrangement.Center) {
items.forEachIndexed { i, item ->
if (i == items.count() / 2) {
Spacer(Modifier.weight(1f))
}
BottomNavigationItem(
icon = {
if (currentRoute == item.route) {
Icon(painterResource(id = item.iconPressed), contentDescription = item.title)
} else {
Icon(painterResource(id = item.iconNormal), contentDescription = item.title)
}
},
selectedContentColor = Color.White,
unselectedContentColor = Color.White,
alwaysShowLabel = false,
selected = currentRoute == item.route,
onClick = {
navController.navigate(item.route) {
navController.graph.startDestinationRoute?.let { route ->
popUpTo(route) {
saveState = true
}
}
launchSingleTop = true
restoreState = true
}
}
)
}
}
}
}
这是我尝试导航到“配置文件”屏幕时的方式(在我的Mainactivity navgraph中):
composable(BottomNavigationItem.Favorites.route) {
FavoriteScreen(navigateToProfile = {
navController.navigate(BottomNavigationItem.Profile.route) {
popUpTo(navController.graph.findStartDestination().id) {
saveState = true
}
launchSingleTop = true
restoreState = true
}
})
}
不幸的是,这给了我一个奇怪的行为,即我的底部navigationbar无法正常工作,主要是第一个。屏幕不再打开了。
某些东西混杂在一起,“ HomeScreen”底部navitem在远程导航后导航到“ profilescreen”。
远程导航的正确方法是什么?
My BottomNavigationBar has 4 items, I want to navigate to the last item when the user clicks a button.
Here is my BottomNavigationBar:
@Composable
fun BottomNavigationBar(navController: NavController) {
val items = listOf(
BottomNavigationItem.Home,
BottomNavigationItem.Explore,
BottomNavigationItem.Favorites,
BottomNavigationItem.Profile
)
val navBackStackEntry by navController.currentBackStackEntryAsState()
val currentRoute = navBackStackEntry?.destination?.route
BottomNavigation(
backgroundColor = colorResource(id = R.color.purple_700),
contentColor = Color.White
) {
Row(horizontalArrangement = Arrangement.Center) {
items.forEachIndexed { i, item ->
if (i == items.count() / 2) {
Spacer(Modifier.weight(1f))
}
BottomNavigationItem(
icon = {
if (currentRoute == item.route) {
Icon(painterResource(id = item.iconPressed), contentDescription = item.title)
} else {
Icon(painterResource(id = item.iconNormal), contentDescription = item.title)
}
},
selectedContentColor = Color.White,
unselectedContentColor = Color.White,
alwaysShowLabel = false,
selected = currentRoute == item.route,
onClick = {
navController.navigate(item.route) {
navController.graph.startDestinationRoute?.let { route ->
popUpTo(route) {
saveState = true
}
}
launchSingleTop = true
restoreState = true
}
}
)
}
}
}
}
and here is how I try to navigate to the "Profile" screen when the user clicks a button (in my MainActivity NavGraph):
composable(BottomNavigationItem.Favorites.route) {
FavoriteScreen(navigateToProfile = {
navController.navigate(BottomNavigationItem.Profile.route) {
popUpTo(navController.graph.findStartDestination().id) {
saveState = true
}
launchSingleTop = true
restoreState = true
}
})
}
Unfortunately this gives me a weird behaviour that my BottomNavigationBar is not working correctly, mostly the first screen is not opening anymore.
Something is getting mixed up and the "HomeScreen" bottomNavItem navigates to the "ProfileScreen" after the remote navigation.
What is the correct way to remotely navigate the bottomNavigationBar?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
经过几个小时的苦苦挣扎,我自己最终创建了a bug Report a href =“ https://github.com/scottschmitz/sample_compose_nav” rel =“ nofollow noreferrer”>示例项目证明了此问题。它被标记为重复,而不是错误。
因此,从那时起,我决定要做的是扩展
navHostController
,并且只始终从我的bottom> bottom> bottomnavigationitem.onclick
的内部调用switchtabs
。我想导致选项卡开关的任何按钮。对我来说,这导致了更加理智的导航结构,并删除了我的所有问题,这些问题在底部磁头上似乎没有反应。
After several hours of struggling with a similar issue myself, I eventually created a bug report and a sample project demonstrating this issue. It was marked as a duplicate and not a bug.
So, what I've decided to do since then is extend
NavHostController
and just always callswitchTabs
from both inside of myBottomNavigationItem.onClick
and from any buttons that I would like to result in the tab switching.For me this has resulted in a much more sane navigation structure and has removed all of my issues where tapping on the bottom nav would appear unresponsive.