AppCompat v7 Toolbar onOptionsItemSelected not called

前端 未结 6 1765
感动是毒
感动是毒 2020-12-14 00:15

I changed from the original ActionBar to the AppCompat Toolbar and setSupportActionBar(toolbar). When I am using getSupportActionBar() and setDisplayHomeAsUpEnabled(true) fo

相关标签:
6条回答
  • 2020-12-14 00:41

    I had to implement an OnClickListener for the DrawerToggle:

    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.

    0 讨论(0)
  • 2020-12-14 00:43

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

    Tested on emulator API 19

    0 讨论(0)
  • 2020-12-14 00:49

    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.

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

    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.

    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.

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

    0 讨论(0)
  • 2020-12-14 00:51

    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();
        }
    });
    
    0 讨论(0)
  • 2020-12-14 00:55

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

    Take a look at the ActionBarDrawerToggle documentation: 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."

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

    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

    0 讨论(0)
  • 2020-12-14 01:01

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

    0 讨论(0)
提交回复
热议问题