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

时间:2018-03-17 14:49:48

标签: java android navigation-drawer

当我按下导航菜单项时,将其颜色变为黄色,直到释放时它被按下。我没有明确写任何代码来突然发生这些事情。我怎么能阻止这个?请帮忙。 这是我的代码。 主要布局 -

     <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 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代码 -

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

当我点击第一项 -

时会发生这种情况

enter image description here

之后发布时没关系

enter image description here

1 个答案:

答案 0 :(得分:0)

尝试添加自定义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;
                }
            });