选择项目后永久导航视图松散焦点 [英] Permanent NavigationView loose focus after an item is selected

查看:137
本文介绍了选择项目后永久导航视图松散焦点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

为了达到这个目的,唯一的修改是在xml布局。这允许NavigationView始终可见。

 <?xml version =1.0encoding =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 =horizo​​ntal>
< 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>
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上,所以任何人都可以测试。 b

GITHUB项目演示

https://github.com/ChristopheVersieux/NavFocus



发生了什么



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



预期结果:

抽屉应该保持专注,否则应该很容易把重点带回到抽屉里。

我试过的东西:



最简单的想法是强制Drawer重新获得焦点,但是这段代码并没有改变任何东西: (){

  navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener(){
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
menuItem.setChecked(true);
//这是我将在右侧区域替换碎片的位置。
navigationView.clearFocus();
navigationView.requestFocus() ;
返回true;
}
});

非常感谢您的帮助。

解决方案

我会开始删除 android:layout_gravity =开始
这是根本不需要,因为它的父母是水平LinearLayout。



导航抽屉必须在平板电脑和电视上永久可见。他们保持隐藏的移动。这些是准则的一部分。



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

本教程的导航抽屉(设计支持)将完全按照最新的材料设计指南为您提供帮助。或者,本教程的项目文件可以在 GitHub 上找到。



更新:
正如指出的那样,支持库v24会在dpad中产生问题。恢复到v23工作得很好。

I started a project with a Navigation Drawer from the basic template of 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. This allow the NavigationView to be always visible.

<?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.

PROJECT DEMO ON GITHUB

https://github.com/ChristopheVersieux/NavFocus

WHAT IS HAPPENING

My issue comes when I start selecting items on the drawer with the 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:

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.

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. 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. Alternatively the project files for the tutorial can be found on GitHub.

UPDATE: As pointed out, Support library v24 creates issues with the dpad. Reverting to v23 works just fine.

这篇关于选择项目后永久导航视图松散焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆