3

i think my problem is actually quite simple, but i can't figure out how to solve it. a have a working navigation drawer with this code:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="5dp" >

    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ListView
            android:id="@+id/category_list_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        </ListView>

    </FrameLayout>

    <!-- The navigation drawer -->
    <ListView 
        android:id="@+id/left_drawer"
        android:entries="@array/features"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="left"
        android:choiceMode="singleChoice"
        android:divider="@null"
        android:background="#E0E0E0"
        android:dividerHeight="0dp"
        />    
</android.support.v4.widget.DrawerLayout>

but when i try do use include like this:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="5dp" >

    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ListView
            android:id="@+id/category_list_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        </ListView>

    </FrameLayout>

    <!-- The navigation drawer -->
    <include layout="@layout/drawer"/>

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

nothing happens when i click on the items of my list view within my frame layout.

this is the drawer layout i'm trying to include:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="5dp" >

    <!-- The navigation drawer -->
    <ListView 
        android:id="@+id/left_drawer"
        android:entries="@array/features"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="left"
        android:choiceMode="singleChoice"
        android:divider="@null"
        android:background="#E0E0E0"
        android:dividerHeight="0dp"
        />

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

thanks for helping!

Rob
  • 2,243
  • 4
  • 29
  • 40
  • I'm considering to add my navigation drawer with just like you did here. Did you do this for performance or memory gains, or just for simplified code? – seekingStillness Oct 09 '18 at 17:01
  • 1
    @seekingStillness I actually did it only for simplified code. I could't say if and how it improves performance :s – Rob Oct 09 '18 at 18:40

2 Answers2

4

Your included layout declares a second android.support.v4.widget.DrawerLayout, it shouldn't. Just declare a ListView in your included layout.

File drawer.xml:

<?xml version="1.0" encoding="utf-8"?>
<ListView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/left_drawer"
    android:entries="@array/features"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="left"
    android:choiceMode="singleChoice"
    android:divider="@null"
    android:background="#E0E0E0"
    android:dividerHeight="0dp"
    />    
David Ferrand
  • 5,357
  • 1
  • 33
  • 43
  • Can you please check this question which is similar : http://stackoverflow.com/questions/32225679/android-including-drawer-in-all-activities-xml-and-calling-them-when-clicked – We are Borg Aug 26 '15 at 12:13
  • I have tried with include and it does not work, I have to paste the view directly in the DrawerLayout which is odd cause the Include view should not change anything – Zapnologica Nov 20 '15 at 18:26
0

I did a RelativeLayout with my items, and the include inside NavigationView.

<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="true"
        android:background="@color/fundo_app"
        tools:visibility="visible">

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

</android.support.design.widget.NavigationView>
cela
  • 2,352
  • 3
  • 21
  • 43