在按钮上浏览底部navbar -jetpack组成

发布于 2025-01-29 02:18:53 字数 2742 浏览 4 评论 0原文

我的底部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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

零度℉ 2025-02-05 02:18:53

经过几个小时的苦苦挣扎,我自己最终创建了a bug Report a href =“ https://github.com/scottschmitz/sample_compose_nav” rel =“ nofollow noreferrer”>示例项目证明了此问题。它被标记为重复,而不是错误。

如果要模拟交换到另一个选项卡,请确保使用与底部NAV所用的完全相同的标志来实际从与另一个选项卡关联的后堆栈关联到与另一个选项卡关联的后堆栈相关联的后堆栈。

因此,从那时起,我决定要做的是扩展navHostController,并且只始终从我的bottom> bottom> bottomnavigationitem.onclick的内部调用switchtabs。我想导致选项卡开关的任何按钮。

fun NavHostController.switchTabs(route: String) {
  navigate(route) {
    // Pop up to the start destination of the graph to
    // avoid building up a large stack of destinations
    // on the back stack as users select items
    popUpTo(graph.findStartDestination().id) {
      saveState = true
    }
    
    // Avoid multiple copies of the same destination when
    // reselecting the same item
    launchSingleTop = true

    // Restore state when reselecting a previously selected item
    restoreState = true
  }
}

对我来说,这导致了更加理智的导航结构,并删除了我的所有问题,这些问题在底部磁头上似乎没有反应。

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.

If you want to emulate swapping to another tab, make sure to use the exact same flags as your BottomNav uses to actually swap from the back stack associated with one tab to the back stack associated with the other tab.

So, what I've decided to do since then is extend NavHostController and just always call switchTabs from both inside of my BottomNavigationItem.onClick and from any buttons that I would like to result in the tab switching.

fun NavHostController.switchTabs(route: String) {
  navigate(route) {
    // Pop up to the start destination of the graph to
    // avoid building up a large stack of destinations
    // on the back stack as users select items
    popUpTo(graph.findStartDestination().id) {
      saveState = true
    }
    
    // Avoid multiple copies of the same destination when
    // reselecting the same item
    launchSingleTop = true

    // Restore state when reselecting a previously selected item
    restoreState = true
  }
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文