【问题标题】:How to avoid the up button with jetpack navigation and bottom tabs?如何避免使用喷气背包导航和底部标签的向上按钮?
【发布时间】:2018-11-11 13:52:01
【问题描述】:

我希望有类似 YouTube 的行为:

  • 有一个工具栏和底部标签
  • 转到“库”(第 5 个选项卡),向上按钮没有出现
  • 进入“我的视频”,出现向上按钮

这是我的限制:

  • 使用喷气背包导航
  • 使用Toolbar(如果重要,请使用AppBarLayout
  • 有底部标签 (BottomNavigationView)
  • 所有底部选项卡都是“顶级”,因为它们不会更改 Toolbar 向上按钮
  • 其他应用程序屏幕不是“顶级”,应该有一个向上箭头
  • 其中一个选项卡是导航图的默认目的地

默认情况下,全部实现后,导航由 Jetpack 导航库自动处理。我使用此助手将工具栏与导航控制器挂钩:

NavigationUI.setupWithNavController(main_toolbar, navController)

这很好用,但是如果我切换底部选项卡之一,向上按钮会出现并按下它会弹回默认目的地。

不使用底部选项卡时这很好,但使用时就尴尬了。

【问题讨论】:

    标签: android android-toolbar bottomnavigationview android-jetpack android-architecture-navigation


    【解决方案1】:

    您可以将AppBarConfiguration 指定为NavigationUI.setupWithNavController 的第三个参数。并将顶级目的地传递给它,如下所示:

    Set<Integer> topLevelDestinations = new HashSet<>();
    topLevelDestinations.add(R.id.navigation_home);
    topLevelDestinations.add(R.id.navigation_trending);
    topLevelDestinations.add(R.id.navigation_subscriptions);
    topLevelDestinations.add(R.id.navigation_inbox);
    topLevelDestinations.add(R.id.navigation_library);
    
    AppBarConfiguration appBarConfiguration = new AppBarConfiguration
            .Builder(topLevelDestinations)
            .build();
    NavigationUI.setupWithNavController(toolbar, navController, appBarConfiguration);
    

    更多详情:AppBarConfiguration

    【讨论】:

    • 接受 BottomNavigation 作为第一个参数的 NavigationUI.setupWithNavController() 没有以 appBarConfiguration 作为第三个参数的版本。这仅适用于 Toolbar 和 CollapsingToolbarLayout 而不适用于 BottomBar。
    猜你喜欢
    • 1970-01-01
    • 2020-03-24
    • 1970-01-01
    • 2019-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-17
    相关资源
    最近更新 更多