首页 > 解决方案 > 导航抽屉 onNavigationItemSelected() 没有响应

问题描述

我在我的应用程序中添加了一个导航抽屉,但是,当我单击导航抽屉中的图标时,它不会执行 onNavigationItemSelected() 方法中定义的操作。相反,它会关闭导航抽屉。如何找到一种方法来确保单击图标时它确实执行 onNavigationItemSelected() 中定义的操作?

屏幕录制GIF

Navigation_Drawer.java

public class Navigation_Drawer extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_navigation__drawer);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.addDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
    setNavigationViewListner();
}

@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.navigation__drawer, menu);
    return true;
}

private void setNavigationViewListner(){
    NavigationView navigationView = (NavigationView)findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement

    return super.onOptionsItemSelected(item);
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
    int id = item.getItemId();
    switch (item.getItemId()){
        case R.id.Logout: {
            User user = new User();
            Toast.makeText(getApplicationContext(), "You logged out", Toast.LENGTH_SHORT).show();
            break;
        }
    }
    if (id == R.id.nav_camera) {
        Toast.makeText(getApplicationContext(), "button has been Clicked", Toast.LENGTH_SHORT).show();
    } else if (id == R.id.nav_gallery) {
        Toast.makeText(getApplicationContext(), "button has been Clicked", Toast.LENGTH_SHORT).show();

    } else if (id == R.id.nav_slideshow) {
        Toast.makeText(getApplicationContext(), "button has been Clicked", Toast.LENGTH_SHORT).show();

    } else if (id == R.id.nav_manage) {
        Toast.makeText(getApplicationContext(), "button has been Clicked", Toast.LENGTH_SHORT).show();

    } else if (id == R.id.Logout) {
        User user = new User();
        Toast.makeText(getApplicationContext(), "You logged out", Toast.LENGTH_SHORT).show(); }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}[![enter image description here][1]][1]}

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:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/drawer_layout"
android:fitsSystemWindows="true"
tools:context="MainActivity"
tools:openDrawer="">

<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
    android:popupTheme = "@style/ThemeOverlay.AppCompat.Light"/>

<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_navigation__drawer"
    app:menu="@menu/activity_navigation__drawer_drawer" />

标签: javaandroidandroid-studionavigation-drawer

解决方案


你确定里面的id和activity_navigation__drawer_drawer.xml里面检查的一样onNavigationItemSelected()吗?
因为还有另一个名称相似的菜单navigation__drawer,我看到你在onCreateOptionsMenu().
这些线的 3d 是多余的:

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
setNavigationViewListner();

它通过调用重复前 2 行setNavigationViewListner()
您还检查了R.id.Logout两次:在声明中switch和稍后else if


推荐阅读