Continuous navigation Free focus after item selection

I started the project with a navigation box from the base Android Studio template. The only thing I did was show it as permanent in order to have a tablet / TV.

To achieve this, the only modification I made was in the xml layout. This allows NavView to always be 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'S HAPPENING

My problem arises when I start to select items in a box using the D-pad. Once an item is selected, focus is completely lost. Trying to go back to the drawer and get focus seems very difficult, and I have to try several times using the left / right arrows

WHAT EXPECTED:

The box should hold focus, or the focus should be easily returned to the box.

WHAT DID I SAY:

I had the simplest idea to get the box to get focus again, but this code does not 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 you very much for your help.

+6
source share
1 answer

I would start by removing android:layout_gravity="start" This is simply not necessary since its parent is a horizontal LinearLayout.

The navigation box should always be visible on tablets and televisions. They remain hidden for mobile devices. They are part of the recommendations.

This navigation box tutorial (design support) will help you with exactly this setup in accordance with the latest material design guidelines. Alternatively, project files for the tutorial can be found on GitHub .

UPDATE: As indicated, the v24 support library creates problems with dpad. Returning to v23 works fine.

0
source

All Articles