繁体   English   中英

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

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

我从Android Studio的基本模板开始了一个带有导航抽屉的项目。 我做的唯一修改是将其显示为永久性以便进行平板电脑/电视布局。

为实现这一目标,我所做的唯一修改是在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

怎么了

当我开始用D-pad选择抽屉上的物品时,我的问题出现了。 选择项目后,焦点将完全丢失。 试图回到抽屉并获得焦点似乎非常困难,我必须用右/左箭头尝试几次

预期的是什么:

抽屉应该保持焦点,或者重点应该很容易带回抽屉。

我做了什么:

我最简单的想法是强制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"开始android:layout_gravity="start"这根本不需要,因为它的父级是一个水平的LinearLayout。

必须在平板电脑和电视上永久显示导航抽屉。 他们隐藏在移动设备中。 这些是材料设计指南的一部分

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

根据最新的Material Design指南, 本导航抽屉(设计支持)教程将帮助您完成该设置。 或者,可以在GitHub上找到本教程的项目文件。

更新:正如所指出的,支持库v24会产生dpad问题。 恢复到v23工作正常。

暂无
暂无

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

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