Ripple effect on NavigationView item

…衆ロ難τιáo~ 提交于 2019-12-23 17:02:05

问题


I have NavigationView in my DrawerLayout and let's say it has simple menu

<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/nav_settings"
        android:icon="@drawable/ic_settings_black_24dp"
        android:title="@string/title_settings"/>

</menu>

Now I set listener for click:

mNavigationVeiw.setOnNavigationItemSelected(this);

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
    switch (item.getItemId()) {
        case R.id.nav_settings:
            SettingsActivity.startActivity(this);
            mDrawerLayout.closeDrawer(GravityCompat.START);
            return true;
    }
    return false;
}

The problem is that I don't see ripple effect. When I long press on item, the ripple is showing, but with click it's not working. I think the problem is that I open Activity after click instead of replacing fragments, and my menu should have addition options. How can I fix that?

EDIT

Everything works fine when I set <group android:checkableBehavior="single"> but in my case it's not this behavior, cause I'm launching another activity and it should not check clicked item, because in this new Activity is back button on the top instead of access navigation drawer.


回答1:


Add itemBackground into NavigationView inside your xml file the following attribute:

app:itemBackground="@drawable/ripple_navigation_selector"

Also, inside the drawable-v21 folder, add ripple_navigation_selector.xml:

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

    <item android:drawable="@color/accentColor" />

    <item
        android:id="@android:id/mask"
        android:drawable="@android:color/white" />

</ripple>


来源:https://stackoverflow.com/questions/39888854/ripple-effect-on-navigationview-item

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