onNavigationItemSelected 未被调用
onNavigationItemSelected is not called
我想在有人点击导航视图时调用 onNavigationItemSelected。代码没有调用 onNavigationItemSelected
方法:
public boolean onNavigationItemSelected(MenuItem menuItem) {
Log.d("Hello","called navigation");
switch (menuItem.getItemId()) {
case R.id.home:
mDrawerLayout.closeDrawer(GravityCompat.START);
break;
default:
mDrawerLayout.closeDrawer(GravityCompat.START);
break;
}
return false;
}
activity_main.xml是:
<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"
tools:context="slidenerd.vivz.navigationviewdemo.MainActivity">
<LinearLayout
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:minHeight="?attr/actionBarSize"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="@+id/main_drawer"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/drawer_header"
app:itemIconTint="@color/colorAccent"
app:itemTextColor="@color/colorTextSecondary"
app:menu="@menu/menu_drawer" />
navigation_menu是:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<!--All the items added without any grouping-->
<item
android:id="@+id/home"
android:icon="@drawable/ic_home"
android:title="@string/home" />
<item
android:id="@+id/statistics"
android:icon="@drawable/ic_trending_up"
android:title="@string/statistics" />
<item
android:id="@+id/navigation_item_3"
android:icon="@drawable/ic_pencil"
android:title="@string/navigation_item_3" />
</menu>
但是,代码不起作用。请帮我解决问题。
您应该设置 NavigationView.setNavigationItemSelectedListener()
你的 activity 工具 NavigationView.OnNavigationItemSelectedListener 吗??
如果是这样,那么您需要该方法是有道理的:
onNavigationItemSelected
尝试将以下命令添加到您的 onCreate 命令中:
navigationView.setNavigationItemSelectedListener(这个);
如果 NavigationView 被称为 navigationView。
在您使用 findViewById
从 activity 中找到它之后
我想在有人点击导航视图时调用 onNavigationItemSelected。代码没有调用 onNavigationItemSelected
方法:
public boolean onNavigationItemSelected(MenuItem menuItem) {
Log.d("Hello","called navigation");
switch (menuItem.getItemId()) {
case R.id.home:
mDrawerLayout.closeDrawer(GravityCompat.START);
break;
default:
mDrawerLayout.closeDrawer(GravityCompat.START);
break;
}
return false;
}
activity_main.xml是:
<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"
tools:context="slidenerd.vivz.navigationviewdemo.MainActivity">
<LinearLayout
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:minHeight="?attr/actionBarSize"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="@+id/main_drawer"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/drawer_header"
app:itemIconTint="@color/colorAccent"
app:itemTextColor="@color/colorTextSecondary"
app:menu="@menu/menu_drawer" />
navigation_menu是:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<!--All the items added without any grouping-->
<item
android:id="@+id/home"
android:icon="@drawable/ic_home"
android:title="@string/home" />
<item
android:id="@+id/statistics"
android:icon="@drawable/ic_trending_up"
android:title="@string/statistics" />
<item
android:id="@+id/navigation_item_3"
android:icon="@drawable/ic_pencil"
android:title="@string/navigation_item_3" />
</menu>
但是,代码不起作用。请帮我解决问题。
您应该设置 NavigationView.setNavigationItemSelectedListener()
你的 activity 工具 NavigationView.OnNavigationItemSelectedListener 吗??
如果是这样,那么您需要该方法是有道理的: onNavigationItemSelected
尝试将以下命令添加到您的 onCreate 命令中: navigationView.setNavigationItemSelectedListener(这个);
如果 NavigationView 被称为 navigationView。
在您使用 findViewById
从 activity 中找到它之后