【问题标题】:Implementing Android Jetpack Navigation component with the new Android BottomAppBar使用新的 Android BottomAppBar 实现 Android Jetpack Navigation 组件
【发布时间】:2019-05-22 16:47:41
【问题描述】:

我正在尝试在我的主要活动中使用新的 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,它应该可以直接工作。

您能否提供一个链接或一些代码来说明如何完成这项工作?

【问题讨论】:

  • 你的R.menu.menu_with_nav_item是什么?
  • 菜单只有一项: 我的问题是导航。我想使用导航喷气背包在底部应用程序栏中的片段之间导航,但我想不出办法

标签: android kotlin android-jetpack android-architecture-navigation android-bottomappbar


【解决方案1】:

Toolbar(或 BottomAppBar 等子类)上的setupWithNavController 仅设置向上图标和标题 - 它们不会关联添加到工具栏的菜单项。

根据Tie destinations to menu items documentation,您必须设置自己的监听器并调用onNavDestinationSelected()。对于BottomAppBar,可以通过设置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)
}

【讨论】:

  • 我将对此进行测试并很快恢复到这个答案。非常感谢您的回答。欣赏!
  • 当我插入以下代码并运行应用程序时,底部AppBar 上的汉堡图标消失了,因此无法访问菜单...您能帮忙吗?
  • 我实际上想使用 Android Jetpack 导航在 Fragment 之间导航,而不是在 BottomAppBar 上使用 BottomSheetDialogFragment ...
  • 如果 setupWithNavController 方法没有按照您的意愿行事(即始终有一个抽屉图标),则您无需使用它们。
  • @ianhanniballake 当我使用此代码时,底部栏会在选择其他项目菜单时自动显示主页按钮。如何禁用此行为?
猜你喜欢
  • 1970-01-01
  • 2021-08-23
  • 2020-09-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多