简体   繁体   English

Android导航抽屉(由Android Studio默认提供)

[英]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 - Java代码-

    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.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;
                }
            });

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

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