简体   繁体   中英

Android navigation drawer(provided by Android studio default)

When I press navigation menu item change it's color yellow till pressed when release it's come into default.I did not write any code explicitly for setting these things sudden it happens. How can I stop this?please help. here is my code. Main Layout-

     <android.support.v4.widget.DrawerLayout
    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"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:state_pressed="false"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_users"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_users"
        app:menu="@menu/activity_users_drawer" />


</android.support.v4.widget.DrawerLayout>

Menu for navigation view-

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

    <group android:checkableBehavior="single">
        <item
            android:id="@+id/nav_satellite"
            android:icon="@drawable/ic_satellite_black_24px"
            android:title="@string/satelliteView" />
        <item
            android:id="@+id/nav_terrain"
            android:icon="@drawable/ic_terrain_black_24px"
            android:title="@string/terrain_nav" />

        <item
            android:id="@+id/nav_logout"
            android:icon="@drawable/ic_power_settings_new_black_24px"
            android:title="@string/logout_button" />


    </group>

    <item android:title="@string/communicateNavLine">
        <menu
            xmlns:android="http://schemas.android.com/apk/res/android">
            <item
                android:id="@+id/nav_share"
                android:icon="@drawable/ic_menu_share"
                android:title="@string/shareNavButton"
                />
            <item
                android:id="@+id/nav_about_us"
                android:icon="@drawable/ic_menu_send"
                android:title="@string/aboutNavButton" />
        </menu>
    </item>

</menu>

java code -

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
    View header=navigationView.getHeaderView(0);
    textViewNavHeader =(TextView)header.findViewById(R.id.text_nav_view);

this is happening when I click the first item-

在此处输入图片说明

After that when release it's ok

在此处输入图片说明

Try to add custom NavigationView.OnNavigationItemSelectedListener

NavigationView navigationView = findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(
            new NavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(MenuItem menuItem) {
                    // set item as selected to persist highlight
                    menuItem.setChecked(false);
                    // close drawer when item is tapped
                    mDrawerLayout.closeDrawers();

                    // Add code here to update the UI based on the item selected
                    // For example, swap UI fragments here

                    return true;
                }
            });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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