Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Navigation view: Scroll the menu items only issue

I want to scroll only the menu items of the navigation view but it also scroll the header with it. Please any body tell me how to achieve this... Here is my code:

activity_main.xml

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
        .......    
        <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/header"
            app:menu="@menu/drawer" />
    </android.support.v4.widget.DrawerLayout>

header.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="190dp"
        android:background="@drawable/background_material"
        android:orientation="vertical"
        >

        <de.hdodenhof.circleimageview.CircleImageView xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/profile_image"
            android:layout_width="76dp"
            android:layout_height="76dp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_centerVertical="true"
            android:layout_marginLeft="24dp"
            android:layout_marginStart="24dp"
            android:src="@mipmap/ic_launcher"
            app:border_color="#FF000000" /
    </RelativeLayout>

drawer_menu_items.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <group android:checkableBehavior="single">

        <item
            android:id="@+id/inbox"
            android:checked="false"
            android:icon="@drawable/ic_inbox_black"
            android:title="@string/inbox_string" />

        <item
            android:id="@+id/starred"
            android:checked="false"
            android:icon="@drawable/ic_star_black"
            android:title="@string/starred_string" />

        <item
            android:id="@+id/sent_mail"
            android:checked="false"
            android:icon="@drawable/ic_send_black"
            android:title="@string/sent_mail_string" />

        <item
            android:id="@+id/drafts"
            android:checked="false"
            android:icon="@drawable/ic_drafts_black"
            android:title="@string/draft_string" />


        <item
            android:id="@+id/allmail"
            android:checked="false"
            android:icon="@drawable/ic_email_black"
            android:title="@string/all_mail_string" />
        <item
            android:id="@+id/trash"
            android:checked="false"
            android:icon="@drawable/ic_delete_black"
            android:title="@string/trash_string" />
        <item
            android:id="@+id/spam"
            android:checked="false"
            android:icon="@drawable/ic_error_black"
            android:title="@string/spam_string" />
        <item
            android:id="@+id/abc"
            android:checked="false"
            android:icon="@android:drawable/ic_menu_camera"
            android:title="Camera" />
        <item
            android:id="@+id/abcd"
            android:checked="false"
            android:icon="@android:drawable/ic_menu_call"
            android:title="Call" />
        <item
            android:id="@+id/abcde"
            android:checked="false"
            android:icon="@android:drawable/ic_menu_gallery"
            android:title="Gallery" />

        <item

            android:checked="false"
            android:icon="@android:drawable/ic_menu_gallery"
            android:title="Gallery" />
        <item

            android:checked="false"
            android:icon="@android:drawable/ic_menu_compass"
            android:title="Compass" />
        <item

            android:checked="false"
            android:icon="@android:drawable/ic_menu_myplaces"
            android:title="My Places" />

    </group>
</menu>

How can I only scroll the menu item not the header... I've attached some pictures too...Before ScrollingAfter Scrolling

like image 791
asad.qazi Avatar asked Jun 19 '15 11:06

asad.qazi


2 Answers

I know its Too old Question but its too easy!

<android.support.design.widget.NavigationView
    android:id="@+id/navigationview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:headerLayout="@layout/header_drawer"
    app:menu="@menu/item_drawer">

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

Add the following code to the NavigationView (<include layout="@layout/header_drawer"/>)

<android.support.design.widget.NavigationView
     android:id="@+id/navigationview"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:layout_gravity="start"
     app:headerLayout="@layout/header_drawer"
     app:menu="@menu/item_drawer">

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

 </android.support.design.widget.NavigationView>
like image 80
Ông Phùng Avatar answered Oct 31 '22 19:10

Ông Phùng


You should be able to place your header outside of the NavigationView, like so:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"

    <!-- nav drawer -->
    <FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        >
        <!-- header -->
        <RelativeLayout 
            android:layout_width="match_parent"
            android:layout_height="190dp"
            android:background="@drawable/background_material"
            android:orientation="vertical"
            >

            <de.hdodenhof.circleimageview.CircleImageView 
                android:id="@+id/profile_image"
                android:layout_width="76dp"
                android:layout_height="76dp"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_centerVertical="true"
                android:layout_marginLeft="24dp"
                android:layout_marginStart="24dp"
                android:src="@mipmap/ic_launcher"
                app:border_color="#FF000000" />
        </RelativeLayout>

        <!-- menu -->
        <android.support.design.widget.NavigationView
            android:id="@+id/navigation_view"
            android:layout_width="wrap_content"
            android:layout_height="0px"
            android:layout_weight="1"
            android:layout_gravity="start"
            app:menu="@menu/drawer" />
    </FrameLayout>
</android.support.v4.widget.DrawerLayout>

Note: I've added a FrameLayout to encapsulate everything in a single view for the DrawerLayout, adjusted the NavigationView's height to automatically use the available space below your header, and have removed the headerLayout attribute.

like image 4
Lorne Laliberte Avatar answered Oct 31 '22 20:10

Lorne Laliberte