简体   繁体   English

选择项目后,Permanent NavigationView松散焦点

[英]Permanent NavigationView loose focus after an item is selected

I started a project with a Navigation Drawer from the basic template of Android Studio. 我从Android Studio的基本模板开始了一个带有导航抽屉的项目。 The only modification I made was to display it as permanent in order to have a tablet/TV layout. 我做的唯一修改是将其显示为永久性以便进行平板电脑/电视布局。

To achieve this, the only modification I made was in the xml layout. 为实现这一目标,我所做的唯一修改是在xml布局中。 This allow the NavigationView to be always visible. 这使得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>

I also put the project on Github, so anyone can test it. 我也将项目放在Github上,所以任何人都可以测试它。

PROJECT DEMO ON GITHUB GITHUB上的项目演示

https://github.com/ChristopheVersieux/NavFocus https://github.com/ChristopheVersieux/NavFocus

WHAT IS HAPPENING 怎么了

My issue comes when I start selecting items on the drawer with the D-pad. 当我开始用D-pad选择抽屉上的物品时,我的问题出现了。 Once an item is selected, the focus is completely lost. 选择项目后,焦点将完全丢失。 Trying to get back to the Drawer and get focus seems very hard and I have to try several times with right/left arrows 试图回到抽屉并获得焦点似乎非常困难,我必须用右/左箭头尝试几次

WHAT IS EXPECTED: 预期的是什么:

Drawer should keep focus, or focus should be easy to bring back to the Drawer. 抽屉应该保持焦点,或者重点应该很容易带回抽屉。

WHAT I TRIED: 我做了什么:

The simplest Idea I had was to force the Drawer to get focus again, but this code doesn't change anything: 我最简单的想法是强制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;
                }
            });

Thank a lot for your help. 非常感谢你的帮助。

I would start by removing android:layout_gravity="start" This is simply not needed as its parent is a horizontal LinearLayout. 我将从删除android:layout_gravity="start"开始android:layout_gravity="start"这根本不需要,因为它的父级是一个水平的LinearLayout。

The Navigation Drawer must be permanently visible on Tablets and TV. 必须在平板电脑和电视上永久显示导航抽屉。 They stay hidden for mobile. 他们隐藏在移动设备中。 These are part of the Material Design guidelines . 这些是材料设计指南的一部分

This requires quite a different setup compared to what I see in your project on GitHub. 与我在GitHub上的项目中看到的相比,这需要完全不同的设置。 Which includes supplying different resources using qualifiers. 其中包括使用限定符提供不同的资源。

This tutorial on Navigation Drawer (Design Support) will help you with exactly that setup, as per the latest Material Design guidelines. 根据最新的Material Design指南, 本导航抽屉(设计支持)教程将帮助您完成该设置。 Alternatively the project files for the tutorial can be found on GitHub . 或者,可以在GitHub上找到本教程的项目文件。

UPDATE: As pointed out, Support library v24 creates issues with the dpad. 更新:正如所指出的,支持库v24会产生dpad问题。 Reverting to v23 works just fine. 恢复到v23工作正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM