android - 使用新的 Android BottomAppBar 实现 Android Jetpack Navigation 组件
问题描述
我正在尝试在我的主要活动中使用新的 Android 底部应用程序栏实现 Jetpack Navigation,但它没有按应有的方式工作。
我已经阅读了关于导航的注释,似乎没有找到任何方法将导航喷气背包集成到新的底部应用程序栏中。我尝试按以下方式进行操作:
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.bottomappbar.BottomAppBar
android:id="@+id/bottom_app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
app:backgroundTint="@color/colorPrimary"
app:fabAlignmentMode="center"
app:fabAttached="true"
app:navigationIcon="@drawable/baseline_menu_white_24"/>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/baseline_add_white_24"
app:layout_anchor="@id/bottom_app_bar" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
导航文件是:
<navigation
android:id="@+id/navigation_graph.xml"
app:startDestination="@id/fragmentStart">
<fragment
android:id="@+id/fragmentStart"
android:name="com.FragmentStart">
<action
android:id="@+id/goToFragment"
app:destination="@id/fragmentToGoTo" />
</fragment>
<fragment
android:id="@+id/fragmentToGoTo"
android:name="com.FragmentToGoTo"
/>
</navigation>
然后在我的活动中我使用:
val navController = Navigation.findNavController(this, R.id.navigation_fragment)
myBottomBar.replaceMenu(R.menu.menu_with_nav_item)
myBottomBar.setupWithNavController(navController)
//or I've also tried
//NavigationUI.setupWithNavController(myBottomBar, navController, null)
发生的情况是,当单击菜单项时,导航不会触发,从我读过的内容来看,如果菜单项与导航图中的片段具有相同的 id,它应该可以直接工作。
您能否通过提供有关如何使其工作的链接或一些代码来提供帮助?
解决方案
setupWithNavController
在 a Toolbar
(或类似的子类BottomAppBar
)上仅设置 Up 图标和标题 - 它们不会连接添加到工具栏的菜单项。
根据Tie destinations to menu items 文档,您必须设置自己的侦听器并调用onNavDestinationSelected()。对于 a BottomAppBar
,可以通过设置 a 来完成Toolbar.OnMenuItemClickListener
:
val navController = Navigation.findNavController(this, R.id.navigation_fragment)
myBottomBar.replaceMenu(R.menu.menu_with_nav_item)
myBottomBar.setupWithNavController(navController)
// Connect MenuItems to the NavController
myBottomBar.setOnMenuItemClickListener { menuItem ->
menuItem.onNavDestinationSelected(navController)
}
推荐阅读
- javascript - 从给定原型获取 json 结果
- python - 高分文件:将字符串列表转换为整数列表会导致 int() 以 10 为底的无效文字:''
- angular - 在角度指令单元测试中注入服务
- sql - 如何将此查询转换为内部联接
- android-gradle-plugin - Android项目更新以及如何解决:“无法解决:监视器”?
- jquery - 更改图中文本和图像的不透明度
- electron - 电子单元测试多窗口
- gradle - Gradle SSH 插件将文件从本地传输到远程 vm
- c++ - 如何将 AERT_Allocate 与 std:vector 一起使用
- linux - 我有一个服务器,我的团队使用相同的用户名。我想追踪谁触发了特定命令?