简体   繁体   English

Android NavigationView菜单图标问题

[英]Android NavigationView menu icon problems

I'm trying to change the icons of the menu items in my navigation view when they are clicked. 我试图在单击导航视图时更改菜单项的图标。 To do this i am using the following method: 为此,我使用以下方法:

private void selectDrawerItem(final MenuItem menuItem){

    for (int i = 0; i < navigationView.getMenu().size(); i++){
        Log.e(TAG, "Clearing " + navigationView.getMenu().getItem(i).getItemId() + " " + navigationView.getMenu().getItem(i).getIcon());
        navigationView.getMenu().getItem(i).getIcon().setTint(ContextCompat.getColor(this, R.color.white));
    }

    switch (menuItem.getItemId()){
        default:
            break;

        case R.id.drawer_item_one:
            menuItem.getIcon().setTint(ContextCompat.getColor(this, R.color.accent));
            Log.e(TAG, "Setting " + menuItem.getItemId() + " " + menuItem.getIcon());
            break;
    }

    Log.e(TAG, "-----END-----");

    menuItem.setChecked(true);
    //drawerLayout.closeDrawers();
}

The problem is that whenever i click the first item, it correctly resets all the icons to white, but then it sets the selected icon to the accent color AND the previous 2 menu items that i clicked. 问题是,每当我单击第一个项目时,它都会将所有图标正确重置为白色,但是随后会将所选图标设置为强调色和我单击的前两个菜单项。 I don't understand what is happening. 我不明白发生了什么。 The logs clearly show that it is only setting the background color one time, so why are 3 of them getting set? 日志清楚地表明只设置了一次背景色,那么为什么要设置其中三个呢?

It seems like any drawer item that i add to the switch statement causes the previous 3 items to also change background colors. 似乎我添加到switch语句的任何抽屉项目都会导致前3个项目也更改背景颜色。 What is happening? 怎么了?

EDIT 编辑

If i replace the code in the switch statement, and just add a delay, it seems to work: 如果我替换了switch语句中的代码,并且只是添加了一个延迟,它似乎可以工作:

case R.id.drawer_item_one:
            new Handler(getMainLooper()).postDelayed(new Runnable(){
                @Override
                public void run(){
                    menuItem.getIcon().setTint(ContextCompat.getColor(context, R.color.primary_dark));
                }
            }, 50);
            break;

I don't want to add a delay though, how can i make this work without the strange workaround? 我不想增加延迟,但是如何在没有奇怪的解决方法的情况下进行这项工作?

EDIT 2 编辑2

Another thing i have noticed is that if instead of changing the color, i change the title text, it works fine. 我注意到的另一件事是,如果我更改标题文本而不是更改颜色,则效果很好。

<android.support.design.widget.NavigationView
        android:id="@+id/navigation_view"
        android:layout_width="@dimen/nav_drawer_width"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:itemIconTint="@drawable/navigation_selected_item"
        app:itemTextColor="@drawable/navigation_selected_item"
        app:menu="@menu/drawer" />

----------------------------------------------------------------
 navigation_selected_item.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- This is used when the Navigation Item is checked -->
    <item android:color="@color/selected_color" android:state_checked="true" />
    <!-- This is the default text color -->
    <item android:color="@color/default_color" />
</selector>

Please refer below link for adding different color for each menu item. 请参考以下链接,为每个菜单项添加不同的颜色。 How to give color to menu items for Navigation drawer? 如何为导航抽屉的菜单项赋予颜色?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM