Android 导航抽屉模板无法工作,除非它在 ​​Emulator AS4.0 上通过 Tab 和光标键导航

Android Navigation Drawer Template Fails to Work Unless it is navigated by tab and cursor keys on Emulator AS4.0

Android 工作室 4。 新项目导航抽屉 Activity。 添加名称等,然后构建。 发送至 Nexus 6P API 28 仿真,Pixel 3a API29 仿真。 还有三星 SM-J530Y Android 9 和诺基亚 7Plus Android 10.

所有实例都允许打开抽屉,所有实例都不允许select打开抽屉,但尝试select一个条目会关闭抽屉。

我在 navController 中添加了一个 onDestinationChangedListener,这只会触发初始主页 selection 事件。

尝试添加 navView.setNavigationItemSelectedListener 但从未触发

搜索只会拖出旧问题,而不会使用更新的 NavController。 尝试更新到最新的 beta01 kotlin 依赖项

def navVersion = "2.3.0-beta01"
implementation "androidx.navigation:navigation-fragment-ktx:$navVersion"
implementation "androidx.navigation:navigation-ui-ktx:$navVersion"

来自模板“2.2.2”

还添加了应用插件:'androidx.navigation.safeargs.kotlin' 无济于事。

会拔掉一些头发,但我没有剩下多少。

我想在我的寻呼机应用程序中添加远离标签的抽屉导航,因此有兴趣让它工作。

正如 Mike M 在评论中强调的那样,activity_main.xml 中的 NavigationView 顺序不正确。

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout 
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:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">

<com.google.android.material.navigation.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_main"
    app:menu="@menu/activity_main_drawer" />

<include
    layout="@layout/app_bar_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
</androidx.drawerlayout.widget.DrawerLayout>

错了。

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout 
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:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">



    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />
</androidx.drawerlayout.widget.DrawerLayout>

有效。 如此简单却又如此令人沮丧。