假期学习记录11(NavigationView点击事件无效)

今天开发碰到一个很恶心的问题,以前能用的NavigationView突然点击事件就没响应了,也算是NavigationView的一个坑吧,特此记录一下。

在我们写滑动布局时会建立一个item然后在主布局中使用NavigationView来调用Item的xml布局,代码如下,我的Item布局名字叫nav_header_main

<?xml version="1.0" encoding="utf-8"?>
<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:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start"
    android:background="@drawable/mainbackground">
   <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <ImageView
                android:src="@drawable/main_run"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
            <TextView
                android:text="运动"
                android:textStyle="bold"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="12dp"
                android:textSize="15dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </LinearLayout>
    <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_main"
        app:menu="@menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>

此时NavigationView点击事件是没有问题的,但是切记不要再NavigationView下面再写布局了,也就是说NavigationView要位于整个布局的最下方,否则NavigationView的点击事件会被下面的布局挡住,就会导致NavigationView中的Item的点击事件无效

原文地址:https://www.cnblogs.com/837634902why/p/12301282.html