NavigationView的点击事件失效原因和解决方案

最近在看一个开源项目的源码, 于是乎想着仿照一下, 自己做一个页面的框架出来, 结果 ,也不知道是这么一回事, 我的Navigation 刚开始点击事件是正常的, 但是, 不知道的...就失效了.....肯定有什么不对!


布局代码是这样的: DrawerLayout + Navigation + CoordinatorLayout


<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.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"
      />

  <!-- 这里include的布局是CoordinatorLayout -->
  <include
      layout="@layout/app_bar_main"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      />

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


乍一看感觉是没毛病的, 但是, nv里面的控件所有点击事件全部都失效了, 明明之前是可以的, 但是就是不知道修改了什么, 就是解决不了, 后来无奈只好重新创建一个工程, 结果nv点击事件是正常的, 我又细细检查了一遍, 终于发现了: 原来我的nv放在了CoordinatorLayout的上面了.....我想, 原因可能是因为CoordinatorLayout这个控件的上下滑动伸缩的效果, 必须保证自己处于布局最上面...

所以, 正确的代码是这样的: 

<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"
    >

  <!-- 这里include的布局是CoordinatorLayout -->
  <include
      layout="@layout/app_bar_main"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      />

 <!-- 注意 nv必须在CoordinatorLayout下面 否则nv点击事件失效 -->
  <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>


为此写一个小小的文章, 为自己也为后人留下一个方便的解决途径


附: 

另外, 顺便写一下nv点击事件的写法 , nv由header+menu组成 , 如果你想实现header里面的控件的点击事件 ,  一般的view v =  nv.findviewxxxx, v.setOnClickxxxxx 是不行的! 正确的代码应该是这样:


情形1: 

你在布局里面的代码跟我的一样, nv里面添加了app:headerLayout 属性, 那么, 代码是这样的: 

 View headerView = nv.getHeaderView(0);
 headerView.findViewById(R.id.nv_header_name).setOnClickListener(this);


情形2: 

如果你在header里面没有添加 app:headerLayout 属性, 那么, 代码是这样的:

View headerView= nv.inflateHeaderView(R.layout.你的header);
headerView.findViewById(R.id.nv_header_name).setOnClickListener(this);





猜你喜欢

转载自blog.csdn.net/yan_startwith2015/article/details/78096247