gpt4 book ai didi

android - 为什么 android jetpack NavigationUI navigateUp 方法仅在您位于导航图的起始目的地时才打开绘图菜单

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:35:55 25 4
gpt4 key购买 nike

NavigationViewDrawLayoutActionBar 一起使用时,jetpack 提供了一些强大的便捷方法来 Hook 一切都放在一起,以便这些项目更容易实现。

NavigationUi 库中提供的方法 setupActionBarWithNavController 是 Activity 的扩展方法,它添加一个汉堡包按钮来打开您的抽屉,如果您覆盖 onSupportNavigateUp: Boolean 方法调用 navigateUp(drawLayout:DrawLayout, navController:NavController) 方法,该方法会将汉堡包按钮更改为后退按钮并自动返回并带有精美的动画,这 super 酷。

不过这个方法好像是这样实现的:

 public static boolean navigateUp(@Nullable DrawerLayout drawerLayout,
@NonNull NavController navController) {
if (drawerLayout != null && navController.getCurrentDestination().getId()
== navController.getGraph().getStartDestination()) {
drawerLayout.openDrawer(GravityCompat.START);
return true;
} else {
return navController.navigateUp();
}
}

如你所见,这个方法的基本逻辑是:

如果您不在导航图的起始目的地,则该按钮为后退按钮,否则为汉堡包按钮

这意味着只有起始目的地可以通过操作栏按钮打开绘图菜单,而所有其他目的地都有一个后退按钮,并且必须滑动才能打开绘图菜单。

为什么会这样?看来这是 android 团队有意识的设计决定。在多个目的地提供汉堡包按钮是否令人不悦?

我希望有几个主分支有汉堡菜单,所有从这些分支出来的屏幕都有一个后退按钮。有没有一种方法可以实现多个 NavGraphs 并将它们链接到一个 NavigationView 中?

你_你

最佳答案

我在 bug 109868820 中报告了在 fragment 内使用工具栏和 NavigationUI 助手时遇到的问题。 .

帮助我的 Googler 明确指出(第 7 篇文章):

[...] the discussions with the material design team has made it clear that the navigation drawer is a global navigation pattern that should be available everywhere

因此 Material 设计抽屉必须在每个屏幕上都可用,包括当导航按钮是“向上”箭头(而不是汉堡包)时更深的抽屉。在这种情况下,抽屉只能通过滑动手势使用,因为导航按钮在应用程序堆栈中向上导航。

确实(错误中的#4):

You'll still get the title set from the android:label in your navigation graph and proper behavior when it comes to the Up button (particularly important if you're using the DrawerLayout versions)

因此您在源代码中发现的行为确实是“正确的”行为。

因此:

  • 顶级目标:通过滑动和汉堡包按钮打开抽屉
  • 非顶级目标:仅通过滑动打开抽屉,按钮向上导航(无汉堡包)

换句话说,汉堡包按钮只能用于顶级目的地。

关于android - 为什么 android jetpack NavigationUI navigateUp 方法仅在您位于导航图的起始目的地时才打开绘图菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50414021/

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