onNavigationItemSelected 和 onOptionsItemSelected 都没有被调用

Neither onNavigationItemSelected nor onOptionsItemSelected is called

我设置了双抽屉布局,正在尝试处理选择抽屉内的项目。不过,我设置它的方式是,当我点击菜单上的某些内容时,我可以看到 onNavigationItemSelected 和 onOptionsItemSelected 都没有被调用(我在函数中放置了一条日志语句)。我也不完全清楚应该调用这些函数中的哪个

编辑:看起来 onOptionsItemSelected() 毕竟被调用了,但无论我点击哪个项目,我总是得到相同的 ID。所以也许它只允许我点击其他图层?

还有很多其他类似的问题,但是 none 的答案对我很有帮助。如果您有任何见解,我将不胜感激。

这是我在 MainActivity.java 中的 onCreate() 函数中的相关代码:

    // Adding Toolbar to Main screen
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);


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

    NavigationView leftNavigationView = (NavigationView) findViewById(R.id.notifications_view);
    leftNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(MenuItem item) {
            Log.i("left", "left"); // this is never called
            // Handle left navigation view item clicks here
            int id = item.getItemId();

            switch(id) {
                case R.id.emerg_con_menuitem:
                    break;
                case R.id.ride_hist_menuitem:
                    Log.i("I'm hit", "I'm hit");
                    intent = new Intent(MainActivity.this, loc_report.class); // The action I want to happen when this menu item is tapped
                    MainActivity.this.startActivity(intent);
                    break;
                case R.id.settings_menuitem:
                    break;
            }

            drawer.closeDrawer(GravityCompat.START);
            return true;
        }
    });

    NavigationView rightNavigationView = (NavigationView) findViewById(R.id.nav_view);
    rightNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(MenuItem item) {
            Log.i("right", "right"); // this is also never called
            // Handle Right navigation view item clicks here.
            int id = item.getItemId();

            Log.i("I am firing", "I am firing");

            switch(id) {
                case R.id.emerg_con_menuitem:
                    break;
                case R.id.ride_hist_menuitem:
                    Log.i("I'm hit navigation", "I'm hit navigation");
                    intent = new Intent(MainActivity.this, loc_report.class);
                    MainActivity.this.startActivity(intent);
                    break;
                case R.id.settings_menuitem:
                    break;
            }

            drawer.closeDrawer(GravityCompat.END);
            return true;
        }
    });

    // Adding menu icon to Toolbar
    ActionBar supportActionBar = getSupportActionBar();
    if (supportActionBar != null) {
        supportActionBar.setHomeAsUpIndicator(R.drawable.ic_notifications);
        supportActionBar.setDisplayHomeAsUpEnabled(true);
        supportActionBar.setDisplayShowTitleEnabled(false);
    }

和被调用的 onOptionsItemSelected(),虽然不在正确的菜单上:

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here.
    Log.i("I'm hit options", "I'm hit options"); // also never called...what is going on??
    int id = item.getItemId();

    switch(id) {
        case R.id.emerg_con_menuitem:
            break;
        case R.id.ride_hist_menuitem:
            Log.i("I'm hit options", "I'm hit options");
            Intent intent = new Intent(MainActivity.this, loc_report.class);
            this.startActivity(intent);
            break;
        case R.id.settings_menuitem:
            break;
        case R.id.menu_navigation:
            drawer.openDrawer(GravityCompat.END); /*Opens the Right Drawer*/
            return true;
    }

    return super.onOptionsItemSelected(item);
}

这是主视图activity_mail.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"
android:fitsSystemWindows="true"
android:background="@drawable/main_background"
style="@style/Theme.AppCompat.DayNight">

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/Protectors"
        app:elevation="0dp">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/Protectors"
            android:textAlignment="center">

        </android.support.v7.widget.Toolbar>

    </android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="end"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/navheader"
    app:menu="@menu/menu_navigation" />

<android.support.design.widget.NavigationView
    android:id="@+id/notifications_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/navheader"
    app:menu="@menu/notifications" />
...

带有打开抽屉的可点击图标的主菜单:

<?xml version="1.0" encoding="utf-8"?>
<menu 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"
    tools:context=".MainActivity">

    <group android:checkableBehavior="single">

        <item // this is the item that's being called on a click
            android:id="@+id/menu_navigation"
            android:icon="@drawable/ic_menu"
            android:title="@string/action_notifications"
            android:orderInCategory="100"
            app:showAsAction="always" />

    </group>
</menu>

抽屉里的东西(右边那个):

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/emerg_con_menuitem"
        android:title="Emergency Contacts"
        />
    <item
        android:id="@+id/ride_hist_menuitem"
        android:title="Ride History" />
    <item
        android:id="@+id/settings_menuitem"
        android:title="Settings" />
</menu>

好的,我想我明白了。我仍然无法使用右抽屉链接,但我可以使用左抽屉链接。这是一个分层问题——所以我点击了 main_menu 项(它包含使抽屉打开的可点击图标)而不是下面的菜单项。为了解决这个问题,我添加了这行代码来修复订单:

NavigationView leftNavigationView = (NavigationView) findViewById(R.id.nav_view);
leftNavigationView.bringToFront(); // <- added this line

然后 OnNavigationItemSelectedListener 实际上按预期触发。