[英]onNavigationItemSelected is not called
I want to call onNavigationItemSelected when someone clicks on navigation view. 当有人点击导航视图时,我想调用onNavigationItemSelected。 The code doesn't call the
onNavigationItemSelected
method: 代码不会调用
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;
}
The activity_main.xml is: 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" />
The navigation_menu is: 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>
But, the code is not working. 但是,代码不起作用。 Please help me to solve the problem.
请帮我解决问题。
你应该设置NavigationView.setNavigationItemSelectedListener()
does your activity implements NavigationView.OnNavigationItemSelectedListener?? 你的活动是否实现了NavigationView.OnNavigationItemSelectedListener?
if it does, it make sense that you need the method: onNavigationItemSelected 如果确实如此,则需要使用方法:onNavigationItemSelected
try to add to your onCreate the command: navigationView.setNavigationItemSelectedListener(this); 尝试添加到onCreate命令:navigationView.setNavigationItemSelectedListener(this);
if the NavigationView is called navigationView. 如果NavigationView被称为navigationView。
after you find it from your activity with findViewById 在您使用findViewById从您的活动中找到它之后
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.