选择项目后永久 NavigationView 失去焦点

Permanent NavigationView loose focus after an item is selected

我从 Android Studio 的基本模板开始了一个带有导航抽屉的项目。我所做的唯一修改是将其显示为永久显示,以便拥有 tablet/TV 布局。

为此,我所做的唯一修改是 xml 布局。这允许 NavigationView 始终可见。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">
    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="300dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <include
            layout="@layout/app_bar_main"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="Content will come here" />
    </LinearLayout>
</LinearLayout>

我也把项目放在Github上了,大家可以测试一下。

项目演示 GITHUB

https://github.com/ChristopheVersieux/NavFocus

发生了什么

当我开始使用方向键选择抽屉中的项目时,我的问题就来了。 一旦选择了一个项目,焦点就完全消失了。试图回到抽屉并获得焦点似乎非常困难,我不得不用 right/left 箭头

尝试几次

预期结果:

Drawer 应该保持焦点,或者焦点应该很容易带回 Drawer。

我尝试了什么:

我最简单的想法是强制抽屉再次获得焦点,但这段代码没有改变任何东西:

navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(MenuItem menuItem) {
                    menuItem.setChecked(true);
                    //This is where I will replace the Fragments in the right area.
                    navigationView.clearFocus();
                    navigationView.requestFocus();
                    return true;
                }
            });

非常感谢您的帮助。

我会先删除 android:layout_gravity="start" 这根本不需要,因为它的父级是水平的 LinearLayout。

导航抽屉必须在平板电脑和电视上永久可见。它们对移动设备保持隐藏状态。这些是 Material 设计的一部分 guidelines

与我在 GitHub 上的项目中看到的相比,这需要完全不同的设置。其中包括使用限定符提供不同的资源。

This tutorial on Navigation Drawer (Design Support) will help you with exactly that setup, as per the latest Material Design guidelines. Alternatively the project files for the tutorial can be found on GitHub.

更新: 正如所指出的,支持库 v24 会导致方向键出现问题。恢复到 v23 工作正常。