2016-08-17 58 views
6

我使用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上,所以任何人都可以測試它。當我開始選擇在與d-墊在抽屜項目

項目DEMO在GitHub上

https://github.com/ChristopheVersieux/NavFocus

發生了什麼

我的問題來了。 選擇一個項目後,焦點完全丟失。試圖讓回到抽屜,並得到重點似乎很努力,我有左/右箭頭

的期望是什麼試幾次:

抽屜應該保持焦點,或重點應該是很容易帶回抽屜。

我的嘗試:

我最簡單的想法是迫使抽屜得到再次集中,但是這個代碼不改變任何東西:

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; 
       } 
      }); 

感謝很多關於你的幫幫我。

+0

您可以上傳圖片以獲得預期的效果和發生的情況嗎? –

回答

0

我會從刪除開始android:layout_gravity="start" 這是根本不需要的,因爲它的父級是水平LinearLayout。

導航抽屜必須在平板電腦和電視上永久可見。他們保持隱藏的移動。這些是材料設計guidelines的一部分。

這需要與我在GitHub上的項目中看到的完全不同的設置。其中包括使用限定符提供不同的資源。根據最新的材料設計指南,

This tutorial on Navigation Drawer (Design Support)將幫助您準確設置該設置。或者,本教程的項目文件可在GitHub上找到。

更新: 正如指出的那樣,支持庫v24會在dpad中產生問題。恢復到v23工作得很好。

+0

你好,你的演示應用程序正在工作,我用你的代碼更新了我的項目,但問題仍然在我的身上發生。我懷疑其他配置問題,而不僅僅是佈局/ Java代碼...仍然調查! https://github.com/ChristopheVersieux/NavFocus –

+1

好吧,這是來自支持庫的錯誤! V23.0.0工作正常! –

+0

更新您的代碼以使用支持v24,它會使用d-pad打破NavigationView行爲。 –

相關問題