material-components / material-components-android

Modular and customizable Material Design UI components for Android
Apache License 2.0
16.35k stars 3.06k forks source link

[NavigationView] Item states not working when there are less than 6 menu items #2735

Closed steamedham544 closed 2 years ago

steamedham544 commented 2 years ago

Description: The ripple animation isn't shown when trying to select an item when one is already selected. This happens if there are less than 6 menu items defined in xml. Adding another item to the menu xml fixes this issue.

Expected behavior: The non-selected items should highlight when touched if an item is already selected.

How to reproduce:

  1. Create a menu xml with 5 items and set it as your menu in NavigationView.
  2. Add an NavigationView.setNavigationItemSelectedListener
  3. Run the app and select an item. Ripples and states work perfectly fine here.
  4. Long press a different item. The touched state doesn't show.
  5. Repeat with 6 menu items and the touched state works perfectly.

Source code:

Non-working menu:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/item_1"
        android:checkable="true"
        android:icon="@drawable/ic_baseline_home_24"
        android:title="Item 1"
        app:showAsAction="ifRoom" />
    <item
        android:id="@+id/item_2"
        android:checkable="true"
        android:icon="@drawable/ic_baseline_home_24"
        android:title="Item 2"
        app:showAsAction="ifRoom" />

    <item
        android:id="@+id/item_3"
        android:checkable="true"
        android:icon="@drawable/ic_baseline_home_24"
        android:title="item_3"
        app:showAsAction="ifRoom" />
    <item
        android:id="@+id/item_4"
        android:checkable="true"
        android:icon="@drawable/ic_baseline_home_24"
        android:title="item_4"
        app:showAsAction="ifRoom" />
    <item
        android:id="@+id/item_5"
        android:checkable="true"
        android:icon="@drawable/ic_baseline_home_24"
        android:title="item_5"
        app:showAsAction="ifRoom" />
</menu>

Working menu:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/item_1"
        android:checkable="true"
        android:icon="@drawable/ic_baseline_home_24"
        android:title="Item 1"
        app:showAsAction="ifRoom" />
    <item
        android:id="@+id/item_2"
        android:checkable="true"
        android:icon="@drawable/ic_baseline_home_24"
        android:title="Item 2"
        app:showAsAction="ifRoom" />

    <item
        android:id="@+id/item_3"
        android:checkable="true"
        android:icon="@drawable/ic_baseline_home_24"
        android:title="item_3"
        app:showAsAction="ifRoom" />
    <item
        android:id="@+id/item_4"
        android:checkable="true"
        android:icon="@drawable/ic_baseline_home_24"
        android:title="item_4"
        app:showAsAction="ifRoom" />
    <item
        android:id="@+id/item_5"
        android:checkable="true"
        android:icon="@drawable/ic_baseline_home_24"
        android:title="item_5"
        app:showAsAction="ifRoom" />

    <item
        android:id="@+id/item_6"
        android:checkable="true"
        android:icon="@drawable/ic_baseline_home_24"
        android:title="item_6"
        app:showAsAction="ifRoom" />
</menu>

Set the listener

binding.navigation.setNavigationItemSelectedListener {
            binding.navigation.setCheckedItem(it)
            true
        }

Android API version: Android 12 and 11

Material Library version: 1.7.0-alpha02

Zelimkhan-Magomadov commented 2 years ago

I'll support you. I also wanted to write about it here. A very unpleasant bug