我一直试图获取导航抽屉中菜单的单击事件,但是它不起作用。
这是我的main_activity
package com.example.administrator.myapplication;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements
NavigationView.OnNavigationItemSelectedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setNavigationViewListener();
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.nav_item_one: {
Intent intent = new Intent(this, first_activity.class);
this.startActivity(intent);
Toast.makeText(Beer.this, "This is my navigation Toast message!",
Toast.LENGTH_LONG).show();
break;
}
case R.id.nav_item_two:{
Intent intent = new Intent(this, second_activity.class);
this.startActivity(intent);
break;
}
}
return true;
}
private void setNavigationViewListener() {
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
}
这是activity_main.xml
<?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_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:theme="@style/AppTheme"
tools:context=".MainActivity">
<include
layout="@layout/app_bar_main"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<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"
android:focusable="true"
app:elevation="20dp"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer">
</android.support.design.widget.NavigationView>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="?attr/actionBarSize">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/main_heading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginBottom="8dp"
android:text="@string/main_heading"
android:textAlignment="center"
android:textColor="@color/pink"
android:textSize="30sp" />
<ImageView
android:id="@+id/header_image"
android:layout_width="wrap_content"
android:layout_height="248dp"
android:layout_below="@id/main_heading"
android:layout_marginBottom="8dp"
android:contentDescription="@string/main"
android:outlineAmbientShadowColor="@color/colorPrimary"
android:src="@drawable/image_header" />
<View
android:id="@+id/divider"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="@id/header_image"
android:background="?android:attr/listDivider" />
<TextView
android:id="@+id/main_description_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/divider"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="8dp"
android:text="@string/beer_description_paragraph_1" />
</RelativeLayout>
</ScrollView>
</android.support.v4.widget.DrawerLayout>
这是activity_main_drawer.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:androud="http://schemas.android.com/apk/res-auto">
<group android:checkableBehavior="single">
<item android:id="@+id/nav_item_one"
android:title="@string/item_one"/>
<item android:id="@+id/nav_item_two"
android:title="@string/item_two"/>
</group>
我可以看到抽屉中的所有项目,当我单击某个项目时,抽屉将关闭,仅此而已,相关联的操作并未执行。
答案 0 :(得分:0)
您需要使用在activity_main_drawer
菜单中声明的相同ID。这样就可以了:
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.nav_beer: {
Intent intent = new Intent(this, first_activity.class);
this.startActivity(intent);
Toast.makeText(Beer.this, "This is my navigation Toast message!",
Toast.LENGTH_LONG).show();
break;
}
case R.id.nav_brandy:{
Intent intent = new Intent(this, second_activity.class);
this.startActivity(intent);
break;
}
}
return true;
}