Ripple effect with itemBackground in NavigationView

I wonder if anyone has problems redefining the background of elements in NavigationView using app:itemBackground"? I get the behavior shown in the screenshot, no matter which element I click on the last element, ripples are displayed instead.

Here is mine drawer_menu.xml:

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

    <group android:checkableBehavior="single" android:id="@+id/first_group">
        <item
            android:id="@+id/nav_home"
            android:icon="@drawable/ic_home"
            android:title="@string/nav_home" />
    </group>

    <group android:id="@+id/second_group">
        <item
            android:id="@+id/nav_settings"
            android:title="@string/nav_settings" />
        <item
            android:id="@+id/nav_about"
            android:title="@string/nav_about" />
        <item
            android:id="@+id/nav_logout"
            android:title="@string/nav_logout" />
    </group>

</menu>

My my_ripple.xml:

<ripple xmlns:android="http://schemas.android.com/apk/res/android"
        android:color="#ffff0000">
    <item
        android:id="@android:id/mask"
        android:drawable="@android:color/white" />
</ripple>

My NavigationView:

<android.support.design.widget.NavigationView
    android:id="@+id/navigation_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="@android:color/white"
    app:headerLayout="@layout/drawer_header"
    app:itemBackground="@drawable/my_ripple"
    app:itemIconTint="@color/drawer_item"
    app:itemTextColor="@color/drawer_item"
    app:menu="@menu/drawer_menu" />

ripple fail

+4
source share
3 answers

Ripple patterns should be defined as follows:

 <!-- A red ripple masked against an opaque rectangle. -->
 <ripple android:color="#ffff0000">
   <item android:id="@android:id/mask"
         android:drawable="@android:color/white" />
 </ripple>

more details

https://developer.android.com/reference/android/graphics/drawable/RippleDrawable.html

0
source

- , ....

addOnGlobalLayoutListener onCreate Drawable .

/**
 * Contains the {@link MenuItem} views in the {@link NavigationView}
 */
private final ArrayList<View> mMenuItems = new ArrayList<>(5);

    // Grab the NavigationView Menu
    final Menu navMenu = mNavigationView.getMenu();
    mNavigationView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            // Remember to remove the installed OnGlobalLayoutListener
            mNavigationView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            // Loop through and find each MenuItem View
            for (int i = 0, length = 5; i < length; i++) {
                final MenuItem item = navMenu.getItem(i);
                mNavigationView.findViewsWithText(mMenuItems, item.getTitle(), View.FIND_VIEWS_WITH_TEXT);
            }
            // Loop through each MenuItem View and apply your custom Typeface
            for (final View menuItem : mMenuItems) {
              //Create RippleDrawable called myRipple 
                ((TextView) menuItem).setBackground(myRipple);
            }
        }
    });
0

Apply the attribute itemBackgroundto menu, not to NavigationView. Then the ripple will remain in the menu item that handles the touch event.

-1
source

All Articles