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