androidnavigation-drawerdrawer

Android drawer selection and scrolling with submenu


I have two problems with the drawer submenu (I use the drawer example generated with the Android Studio wizard)

First problem

I try to select de subitems, for example if I click on "Tools" the selecteItem "effect" its works fine, but when I try to click on "Share" or "Send" and the selectedItem "effect" is not works:

(This is crazy because is the Android example, I suppose that this is an official example and should be work fine) enter image description here

Second problem

I have another example which have a lot of items (for example 15 items with subitems), so when I click on the last, after that, when I try to open the drawer, this is automatically scroll to top. son I can not see the last item selected. I need that the scroll works automatically to the selected item. Is this possible?

Example code

<?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/nav_camera"
            android:icon="@drawable/ic_menu_camera"
            android:title="Import" />
        <item
            android:id="@+id/nav_gallery"
            android:icon="@drawable/ic_menu_gallery"
            android:title="Gallery" />
        <item
            android:id="@+id/nav_slideshow"
            android:icon="@drawable/ic_menu_slideshow"
            android:title="Slideshow" />
        <item
            android:id="@+id/nav_manage"
            android:icon="@drawable/ic_menu_manage"
            android:title="Tools" />
    </group>

    <item android:title="Communicate">
        <menu>
            <item
                android:id="@+id/nav_share"
                android:icon="@drawable/ic_menu_share"
                android:title="Share" />
            <item
                android:id="@+id/nav_send"
                android:icon="@drawable/ic_menu_send"
                android:title="Send" />
        </menu>
    </item>

</menu>

Solution

  • for your first problem just add group to your items android:checkableBehavior like first item group

    <item android:title="Communicate">
            <menu>
         <group android:checkableBehavior="single">
                <item
                    android:id="@+id/nav_share"
                    android:icon="@drawable/ic_menu_share"
                    android:title="Share" />
                <item
                    android:id="@+id/nav_send"
                    android:icon="@drawable/ic_menu_send"
                    android:title="Send" />
           </group>
            </menu>
        </item>
    

    and for second problem as for as i know it maintain selected item automatically check this after changing the android:checkableBehavior