How to add one section separator for Navigation Drawer in Android?

假装没事ソ 提交于 2019-11-29 19:04:29
espinchi

Make sure you define each group with a unique ID, separator won't appear without the ID.

For example, this is my drawer_menu.xml:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <group
        android:id="@+id/menu_top"
        android:checkableBehavior="single">
        <item
            android:checked="true"
            android:id="@+id/drawer_item_timeline"
            android:icon="@drawable/ic_timer_grey600_24dp"
            android:title="@string/drawer_timeline"/>
        <item
            android:id="@+id/drawer_item_reports"
            android:icon="@drawable/ic_timetable_grey600_24dp"
            android:title="@string/drawer_reports"/>
    </group>

    <group
        android:id="@+id/menu_bottom"
        android:checkableBehavior="none">

        <item
            android:id="@+id/drawer_item_settings"
            android:icon="@drawable/ic_settings_black_24dp"
            android:title="@string/drawer_settings" >
        </item>
    </group>
</menu>

Gabriel adds below in the comments that if the group doesn't have an id, the separator will not appear.

To separate menu items by a divider line, only group items with a unique id like following example:

activity_main_drawer.xml:

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

    <item
        android:id="@+id/nav_apps_and_games"
        android:icon="@drawable/ic_apps_black_24dp"
        android:title="@string/my_apps_and_games" />

    <item
        android:id="@+id/nav_bookmarked_apps"
        android:icon="@drawable/ic_add_bookmark_black_24dp"
        android:title="@string/bookmarked_apps" />

    <item
        android:id="@+id/nav_manage_downloads"
        android:icon="@drawable/ic_downloading_file_black_24dp"
        android:title="@string/manage_downloads" />

    <!-- SET A UNIQUE ID TO THE BELOW GROUP -->
    <group android:id="@+id/group1">

        <item
            android:id="@+id/nav_settings"
            android:icon="@drawable/ic_settings_black_24dp"
            android:title="@string/settings" />

        <item
            android:id="@+id/nav_sign_up"
            android:icon="@drawable/ic_card_membership_black_24dp"
            android:title="@string/sign_up_login" />

    </group>

</menu>

Visual Result:

My hacky method is similar to Mostrapotski's.

In my Layout for my custom adapter, I'm adding a horizontal separator at the beginning of each item and setting it's visibility to gone.

For the elements that mark the beginning of a new group, I set their visibility to visible so that the separator shows up on top of it.

You have two choices

  1. Your items can be separated (a list at the top, and classic views at the bottom). Then instead of the listview in your main layout (android:id="@+id/left_drawer") you can have a rather complex LinearLayout including those 3 items (list, separator, and bottom views)
  2. Your items must be exactly as in your example, then you need the separator in the list, you can use some logic in your adapter to draw a view on top of the list item where you need the separator. (meaning your list item won't be a single textview anymore, but a LinearLayout with a gone separator (and visible sometimes, according to your adapter's logic).

To help you with some sample code, can you please post all the items you need in your menu ? We need to know exactly what will be static and what will be scrollable.

Edit: If you want that working with the exemple, get rid of the line

mDrawerList.setAdapter(new ArrayAdapter<String>(this, ...)

You need to supply a home made adapter like this: https://github.com/codepath/android_guides/wiki/Using-an-ArrayAdapter-with-ListView

As i said in 2, in your adapter, you will have logic, and thus you can say in the getView() method

if(myPlanet.isNeptune()) 
    holder.mSepatator.setVisibility(View.VISIBLE);
else 
    holder.mSepatator.setVisibility(View.GONE);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!