这是一个小坑,就是你自己的布局有问题, 在Xml中NavigationView必须位于最下方 :
<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <Button android:id="@+id/et" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:hint="申请权限"/> </LinearLayout> <FrameLayout android:id="@+id/fl_container_left" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="left"> <include layout="@layout/navigate"/> // 放置在最下面 </FrameLayout> </android.support.v4.widget.DrawerLayout>
这样做之后你的条目点击事件才会有反应,否则,不会走点击事件.
@Override public boolean onNavigationItemSelected(@NonNull MenuItem item) { int id = item.getItemId(); switch (id) { case R.id.mn_home: Toast.makeText(MainActivity.this, "首页", Toast.LENGTH_SHORT).show(); break; case R.id.mn_download: //showToast("我的信息"); Toast.makeText(MainActivity.this, "我的信息", Toast.LENGTH_SHORT).show(); break; case R.id.mn_store: // showToast("猜你喜欢"); Toast.makeText(MainActivity.this, "猜你喜欢", Toast.LENGTH_SHORT).show(); break; case R.id.mn_personal: // showToast("电影"); Toast.makeText(MainActivity.this, "电影", Toast.LENGTH_SHORT).show(); break; case R.id.mn_setting: //showToast("音乐"); Toast.makeText(MainActivity.this, "音乐", Toast.LENGTH_SHORT).show(); break; } return true; // 注意这里返回的是return true }
注意 : 上面返回的是return true