NavigationView: how to insert divider without subgroup?

你。 提交于 2019-11-27 14:37:00

All you need to do is define a group with an unique ID. I looked through NavigationView's implementation, and it will create a divider every time the group id is changed.

e.g.

<group android:id="@+id/my_id">
    <!-- Divider will appear above this item -->
    <item ... />
</group>

I tried this and it worked

See implementation from line no 122-125

This is my menu file and it is creating the separator:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".MyFirstMNCActivity">
    <group android:id="@+id/grp1">
        <item
            android:id="@+id/navigation_item_1"
            android:checked="true"
            android:icon="@drawable/ic_home"
            android:title="@string/navigation_item_1" />

    </group>

    <group android:id="@+id/grp2">

        <item
            android:id="@+id/navigation_item_2"
            android:icon="@drawable/ic_home"
            android:title="@string/navigation_item_2" />
    </group>
</menu>
NavigationView navigationView = (NavigationView) findViewById(R.id.navigation);
NavigationMenuView navMenuView = (NavigationMenuView) navigationView.getChildAt(0);
navMenuView.addItemDecoration(new DividerItemDecoration(appContext,DividerItemDecoration.VERTICAL_LIST));

Here is the gist of DividerItemDecoration

DividerItemDecoration.java

Using xml

<android.support.design.widget.NavigationView
...
app:itemIconTint="@android:color/black" 
... />

Programmatically

 yourNavigationView.setItemIconTintList(null);

I feel that is good answer.

Best Luck.

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