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

前端 未结 6 1667
粉色の甜心
粉色の甜心 2020-12-22 20:43

I have a navigation drawer like this image. I want to add a section separator (like the line separating Neptune). It seems simple but I can\'t find anything on the web that

相关标签:
6条回答
  • 2020-12-22 21:03

    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);
    
    0 讨论(0)
  • 2020-12-22 21:04

    Add group in Menu file to add divider enter image description here

        </item>
    </group>
    
    0 讨论(0)
  • 2020-12-22 21:05

    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:

    0 讨论(0)
  • 2020-12-22 21:14

    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.

    0 讨论(0)
  • 2020-12-22 21:15

    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.

    0 讨论(0)
  • 2020-12-22 21:17

    Add Group to add divider in navigation drawer

        </item>
    </group>
    
    0 讨论(0)
提交回复
热议问题