2015-06-22 17 views
10

Ich verwende das neue NavigationView von Revision 22.2.0 des support library von Google. Es funktioniert einwandfrei, um eine Navigationsleiste zu erstellen, die über ein Menü res ausgefüllt wird.Hinzufügen eines ListView oder RecyclerView zu einem neuen NavigationView

Ich frage mich, ob es möglich ist, eine ListView oder RecyclerView in die Navigationsleiste hinzuzufügen, so dass sie mit meinem benutzerdefinierten Adapter Code gefüllt werden kann, der viel größere Flexibilität als Menüressourcen ermöglicht.

Hier ist meine aktuelle XML:

<?xml version="1.0" encoding="utf-8"?> 

<android.support.v4.widget.DrawerLayout 
    android:id="@+id/drawer_layout" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:context=".MainActivity"> 

    <FrameLayout 
     android:id="@+id/content_view" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 

     <include layout="@layout/main_toolbar" /> 

    </FrameLayout> 

    <android.support.design.widget.NavigationView 
     android:id="@+id/navigation_view" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     app:headerLayout="@layout/navigation_drawer_header" 
     app:menu="@menu/menu_navigation_drawer" /> 


</android.support.v4.widget.DrawerLayout> 

Wo in meinem XML würde ich die Listview oder RecyclerView hinzufügen?

EDIT

Per Basant Vorschlag, verschachtelt ich ein Listview in die NavigationView. Du verlierst die Fähigkeit, aus einem Menü res aufzublähen (so weit ich weiß), aber es gelingt mir, was ich will. Das Header-XML ist unverändert, es ist nur in XML enthalten.

Neuer Code:

<?xml version="1.0" encoding="utf-8"?> 

<android.support.v4.widget.DrawerLayout 
    android:id="@+id/drawer_layout" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:context=".MainActivity"> 

    <FrameLayout 
     android:id="@+id/content_view" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 

     <include layout="@layout/main_toolbar" /> 

    </FrameLayout> 

    <android.support.design.widget.NavigationView 
     android:id="@+id/navigation_view" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_gravity="start"> 

     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 

      <include 
       android:id="@+id/navigation_drawer_header_include" 
       layout="@layout/navigation_drawer_header" /> 

      <ListView 
       android:id="@+id/navigation_drawer_list" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_below="@id/navigation_drawer_header_include"/> 

     </RelativeLayout> 

    </android.support.design.widget.NavigationView> 


</android.support.v4.widget.DrawerLayout> 

Antwort

14

Sie können nur Nest der ListView oder RecyclerView innerhalb des NavigationView.

<?xml version="1.0" encoding="utf-8"?> 

<android.support.v4.widget.DrawerLayout 
    android:id="@+id/drawer_layout" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:context=".MainActivity"> 

    <FrameLayout 
     android:id="@+id/content_view" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 

     <include layout="@layout/main_toolbar" /> 

    </FrameLayout> 

    <android.support.design.widget.NavigationView 
     android:id="@+id/navigation_view" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_gravity="start"/> 

     <ListView 
      android:id="@+id/menuList" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"/> 
</android.support.v4.widget.DrawerLayout> 

HINWEIS: Beachten Sie, dass, wenn Sie ein Listview im Inneren verwenden verwenden, können Sie nicht die Kopfzeile des NavigationView verwenden können. Sie müssen die Kopfzeile der hinzuzufügenden ListView verwenden. Vergessen Sie nicht, die Felder app:menu und app:header zu entfernen.

+0

Hallo, habe ich versucht, dass aber die nav Schublade Pausen im Sinne der Navigationselemente sind nicht mehr anklickbar. Habe gerade deinen NOTE-Schnitt gesehen, ich werde es jetzt versuchen. – nabir

+2

Ich denke, Sie sollten das NavigationMenu in diesem Fall nicht verwenden. Verwenden Sie einfach ein ListView oder ein komplexeres Layout, wenn Sie anstelle eines NavigationsMenu benötigen. Das gibt Ihnen mehr Flexibilität. –

+0

Der Grund, warum ich NavigationView verwenden möchte, ist die eingebaute Scrim-Handhabung, um mehr Material zu bekommen. Ich habe deinen Vorschlag angewendet und es scheint zu funktionieren. Ich werde meinen Beitrag aktualisieren, um den neuen Code anzuzeigen. – nabir

15

Wenn Sie innerhalb der NavigationView Ansichten hinzufügen möchten, können Sie so etwas tun. Auf diese Weise haben Sie keine Einschränkungen beim Hinzufügen von Header in Ihrer NavigationView mit ListView.

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/drawer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:openDrawer="start"> 

    <include 
     layout="@layout/app_bar_main" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

    <android.support.design.widget.NavigationView 
     android:id="@+id/nav_view" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:fitsSystemWindows="false" 

     > 
     <LinearLayout 
      android:orientation="vertical" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 
      <include layout="@layout/nav_header_main" 
       android:id="@+id/my" 
       /> 
      <ListView 

       android:layout_weight="7" 
       android:layout_width="match_parent" 
       android:layout_height="0dp" 
       android:id="@+id/list_view_inside_nav"></ListView> 
     </LinearLayout> 
    </android.support.design.widget.NavigationView> 

</android.support.v4.widget.DrawerLayout> 

, dass in etwa so aussehen

example

+0

Dies wird nicht scrollen die Header-Ansicht wie die Navigationsansicht tut – Killer

+1

ja, können Sie sie in lineares Layout mit Ausrichtung vertikal und dann in scrollview mit Parameter fillViewPort = true – deadfish

+1

Es versucht, aber AFAIK (erinnern), wenn ich die Navigation zu öffnen Schublade mit Recycler-Ansicht statt Listenansicht In der Ansicht wird die Kopfzeile nicht angezeigt, anstatt in die Recycler-Ansicht zu scrollen. @deadfish – Killer

2

Als Reaktion auf Shubham ‚s Kommentar

This will not scroll the header view like the Navigation View does

ich es gelöst, indem die LinearLayout in einem NestedScrollView setzen. Jetzt scrollt es richtig mit dem Header.

<android.support.v4.widget.NestedScrollView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 

     <include layout="@layout/nav_header_main" /> 

     <android.support.v7.widget.RecyclerView 
      android:id="@+id/nav_list" 
      android:layout_width="match_parent" 
      android:layout_height="@dimen/weight_based_height" 
      android:layout_weight="1" 
      android:nestedScrollingEnabled="false"/> 
    </LinearLayout> 

</android.support.v4.widget.NestedScrollView> 
1

***** versuchen auf diese Weise *****

<android.support.design.widget.NavigationView 
android:id="@+id/navigation" 
android:layout_width="wrap_content" 
android:layout_height="match_parent" 
android:layout_gravity="start" 
android:fitsSystemWindows="true"> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <include layout="@layout/nav_header" /> 

    <ListView 
     android:id="@+id/lst_menu_items" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" /> 
</LinearLayout> 

Verwandte Themen