Android AppCompat v7 工具栏 onOptionsItemSelected 未调用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26588917/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:55:13  来源:igfitidea点击:

AppCompat v7 Toolbar onOptionsItemSelected not called

androidandroid-actionbartoolbarandroid-5.0-lollipopandroid-appcompat

提问by Informatic0re

I changed from the original ActionBar to the AppCompat Toolbar and setSupportActionBar(toolbar). When I am using getSupportActionBar() and setDisplayHomeAsUpEnabled(true) for the back arrow, the click never calls onOptionsItemSelected or any other listener method.

我从原来的 ActionBar 改为 AppCompat Toolbar 和 setSupportActionBar(toolbar)。当我使用 getSupportActionBar() 和 setDisplayHomeAsUpEnabled(true) 作为后退箭头时,单击永远不会调用 onOptionsItemSelected 或任何其他侦听器方法。

Do I have to implement some special listener for it? Befor everything was working just fine.

我是否必须为它实现一些特殊的监听器?之前一切正常。

EDIT: Initialise the ActionBar:

编辑:初始化 ActionBar:

mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
mActionBar = getSupportActionBar();
mActionBar.setHomeButtonEnabled(true);

and after replacing the content with a Fragment I do this:

在用 Fragment 替换内容后,我这样做:

mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
mDrawerToggle.setDrawerIndicatorEnabled(false);
mActionBar.setDisplayHomeAsUpEnabled(true);

回答by Andrei Lupsa

I know this question has been answered but I found the real cause of the problem after 2 days of frustration.

我知道这个问题已经得到解答,但我在沮丧 2 天后找到了问题的真正原因。

Take a look at the ActionBarDrawerToggle documentation: https://developer.android.com/reference/android/support/v7/app/ActionBarDrawerToggle.html

看看 ActionBarDrawerToggle 文档:https: //developer.android.com/reference/android/support/v7/app/ActionBarDrawerToggle.html

Notice the two constructors there. My mistake was that I was using the second constructor that was taking a toolbar as a parameter. It took me so long to notice the last line in the consturctor documentation: "Please use ActionBarDrawerToggle(Activity, DrawerLayout, int, int) if you are setting the Toolbar as the ActionBar of your activity."

注意那里的两个构造函数。我的错误是我使用了将工具栏作为参数的第二个构造函数。我花了很长时间才注意到构造器文档中的最后一行:“如果您将工具栏设置为活动的 ActionBar,请使用 ActionBarDrawerToggle(Activity, DrawerLayout, int, int)

After using the first constructor onOptionsItemSelected() was called with no issues.

使用第一个构造函数后 onOptionsItemSelected() 被调用没有问题。

Don't forget to call the ActionBarDrawerToggle.onConfigurationChanged()and onOptionsItemSelected()from your activity as described in the last part here: http://developer.android.com/training/implementing-navigation/nav-drawer.html

不要忘记从您的活动中调用ActionBarDrawerToggle.onConfigurationChanged()onOptionsItemSelected(),如此处最后一部分所述:http: //developer.android.com/training/implementing-navigation/nav-drawer.html

回答by Informatic0re

I had to implement an OnClickListener for the DrawerToggle:

我必须为 DrawerToggle 实现一个 OnClickListener:

mDrawerToggle.setToolbarNavigationClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        popStackIfNeeded();
        mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
        mActionBar.setDisplayHomeAsUpEnabled(false);
        mDrawerToggle.setDrawerIndicatorEnabled(true);
    }
});

this fixed my issue.

这解决了我的问题。

回答by reactive-core

I had several issues using the setSupportActionBar() method. It also ignores certain color themes, so you can't style the back arrow or overflow icon (don't remember which). I just did away with ActionBar integration and use the Toolbar natively. So, as an alternative, you could do that as follows.

我在使用 setSupportActionBar() 方法时遇到了几个问题。它还会忽略某些颜色主题,因此您无法设置后退箭头或溢出图标的样式(不记得是哪个)。我只是取消了 ActionBar 集成并在本机使用工具栏。因此,作为替代方案,您可以按如下方式执行此操作。

Just include the toolbar like you would normally, in your layout, assume it's using an id of @+id/toolbar.

只需像往常一样在布局中包含工具栏,假设它使用@+id/toolbar 的 id。

Then, in code:

然后,在代码中:

_toolbar = (Toolbar) findViewById(R.id.toolbar);
_toolbar.setNavigationOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        handleNavButtonPress();
    }
});
_toolbar.setOnMenuItemClickListener(_menuItemClickListener);
_toolbar.inflateMenu(R.menu.message_list_menu);
Menu menu = _toolbar.getMenu();

In this case, _menuItemClickListener can almost literally be your current onOptionsItemSelected() method renamed. You just don't have to check for menu being null anymore.

在这种情况下, _menuItemClickListener 几乎可以真正重命名您当前的 onOptionsItemSelected() 方法。您只需不再检查菜单是否为空。

To remove items from the menu, just call menu->clear(). So in my onPause, I clear the menus and onResume, I inflate them, in my fragments, and each fragment sets the click handler in onResume. You need to always clean up, because Android won't do that for you in this approach, and the toolbar will keep adding menus every time you inflate.

要从菜单中删除项目,只需调用 menu->clear()。所以在我的 . 中onPause,我清除了菜单和 onResume,我在我的片段中对它们进行了膨胀,并且每个片段在 onResume 中设置了单击处理程序。您需要始终进行清理,因为在这种方法中,Android 不会为您执行此操作,并且每次充气时工具栏都会不断添加菜单。

One last note, to make it all work, you have to disable the action bar completely and remove it from the style.

最后一个注意事项,要使其一切正常,您必须完全禁用操作栏并将其从样式中删除。

回答by Max Izrin

One thing that wasn't mentioned:
If you build the options menu dynamically in onCreateOptionsMenuand return nullthere, the up button in the action bar will not work.
Works fine if you return the Menu parameter without adding anything into it.

没有提到的一件事:
如果您在onCreateOptionsMenu 中动态构建选项菜单并在那里返回null,则操作栏中的向上按钮将不起作用。
如果您返回 Menu 参数而不添加任何内容,则效果很好。

Tested on emulator API 19

在模拟器 API 19 上测试

回答by Prof

If you've tried everything and it just doesn't work, you can implement your own click listener like so:

如果您已经尝试了所有方法但它不起作用,您可以像这样实现自己的点击侦听器:

myNavList.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        String item = myNavList.getItemAtPosition(position).toString();
        Toast.makeText(this, "You selected " + item, Toast.LENGTH_SHORT).show();
    }
});

回答by Coder Absolute

In my case the setHasOptionsMenu(true);wasn't enabled on onCreateView. Hope this helps someone.

在我的情况下,setHasOptionsMenu(true);未启用onCreateView. 希望这可以帮助某人。