Cannot catch toolbar home button click event

前端 未结 11 1953
悲&欢浪女
悲&欢浪女 2020-11-27 02:56

I\'ve implemented the newest appcompat library and using the Toolbar as action bar. But the problem is I cannot catch the home button / hamburger icon click eve

相关标签:
11条回答
  • 2020-11-27 03:11
        mActionBarDrawerToggle = mNavigationDrawerFragment.getActionBarDrawerToggle();
        mActionBarDrawerToggle.setToolbarNavigationClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // event when click home button
            }
        });
    

    in mycase this code work perfect

    0 讨论(0)
  • 2020-11-27 03:13

    The easiest approach we could do is change the home icon to a known icon and compare drawables (because android.R.id.home icon can differ to different api versions

    so set a toolbar as actionbar SetSupportActionBar(_toolbar);

    _toolbar.NavigationIcon = your_known_drawable_here;
    
       for (int i = 0; i < _toolbar.ChildCount; i++)
                {
                    View v = _toolbar.GetChildAt(i);
                    if (v is ImageButton)
                    {
                        ImageButton imageButton = v as ImageButton;
    
                        if (imageButton.Drawable.GetConstantState().Equals(_bookMarkIcon.GetConstantState()))
                        {
                           //here v is the widget that contains the home  icon you can add your click events here 
                        }
                    }
                }
    
    0 讨论(0)
  • 2020-11-27 03:15

    If you want to know when home is clicked is an AppCompatActivity then you should try it like this:

    First tell Android you want to use your Toolbar as your ActionBar:

    setSupportActionBar(toolbar);
    

    Then set Home to be displayed via setDisplayShowHomeEnabled like this:

    getSupportActionBar().setDisplayShowHomeEnabled(true);
    

    Finally listen for click events on android.R.id.home like usual:

    @Override
    public boolean onOptionsItemSelected(MenuItem menuItem) {
        if (menuItem.getItemId() == android.R.id.home) {
            Timber.d("Home pressed");
        }
        return super.onOptionsItemSelected(menuItem);
    }
    

    If you want to know when the navigation button is clicked on a Toolbar in a class other than AppCompatActivity you can use these methods to set a navigation icon and listen for click events on it. The navigation icon will appear on the left side of your Toolbar where the the "home" button used to be.

    toolbar.setNavigationIcon(getResources().getDrawable(R.drawable.ic_nav_back));
    toolbar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d("cek", "home selected");
        }
    });
    

    If you want to know when the hamburger is clicked and when the drawer opens, you're already listening for these events via onDrawerOpened and onDrawerClosed so you'll want to see if those callbacks fit your requirements.

    0 讨论(0)
  • 2020-11-27 03:17

    I changed the DrawerLayout a bit to get the events and be able to consume and event, such as if you want to use the actionToggle as back if you are in detail view:

    public class ListenableDrawerLayout extends DrawerLayout {
    
        private OnToggleButtonClickedListener mOnToggleButtonClickedListener;
        private boolean mManualCall;
    
        public ListenableDrawerLayout(Context context) {
            super(context);
        }
    
        public ListenableDrawerLayout(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public ListenableDrawerLayout(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
        }
    
        /**
         * Sets the listener for the toggle button
         *
         * @param mOnToggleButtonClickedListener
         */
        public void setOnToggleButtonClickedListener(OnToggleButtonClickedListener mOnToggleButtonClickedListener) {
            this.mOnToggleButtonClickedListener = mOnToggleButtonClickedListener;
        }
    
        /**
         * Opens the navigation drawer manually from code<br>
         * <b>NOTE: </b>Use this function instead of the normal openDrawer method
         *
         * @param drawerView
         */
        public void openDrawerManual(View drawerView) {
            mManualCall = true;
            openDrawer(drawerView);
        }
    
        /**
         * Closes the navigation drawer manually from code<br>
         * <b>NOTE: </b>Use this function instead of the normal closeDrawer method
         *
         * @param drawerView
         */
        public void closeDrawerManual(View drawerView) {
            mManualCall = true;
            closeDrawer(drawerView);
        }
    
    
        @Override
        public void openDrawer(View drawerView) {
    
            // Check for listener and for not manual open
            if (!mManualCall && mOnToggleButtonClickedListener != null) {
    
                // Notify the listener and behave on its reaction
                if (mOnToggleButtonClickedListener.toggleOpenDrawer()) {
                    return;
                }
    
            }
            // Manual call done
            mManualCall = false;
    
            // Let the drawer layout to its stuff
            super.openDrawer(drawerView);
        }
    
        @Override
        public void closeDrawer(View drawerView) {
    
            // Check for listener and for not manual close
            if (!mManualCall && mOnToggleButtonClickedListener != null) {
    
                // Notify the listener and behave on its reaction
                if (mOnToggleButtonClickedListener.toggleCloseDrawer()) {
                    return;
                }
    
            }
            // Manual call done
            mManualCall = false;
    
            // Let the drawer layout to its stuff
            super.closeDrawer(drawerView);
        }
    
        /**
         * Interface for toggle button callbacks
         */
        public static interface OnToggleButtonClickedListener {
    
            /**
             * The ActionBarDrawerToggle has been pressed in order to open the drawer
             *
             * @return true if we want to consume the event, false if we want the normal behaviour
             */
            public boolean toggleOpenDrawer();
    
            /**
             * The ActionBarDrawerToggle has been pressed in order to close the drawer
             *
             * @return true if we want to consume the event, false if we want the normal behaviour
             */
            public boolean toggleCloseDrawer();
        }
    
    }
    
    0 讨论(0)
  • 2020-11-27 03:18

    In my case I had to put the icon using:

    toolbar.setNavigationIcon(R.drawable.ic_my_home);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayShowHomeEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);
    

    And then listen to click events with default onOptionsItemSelected and android.R.id.home id

    0 讨论(0)
  • 2020-11-27 03:20

    This is how I do it to return to the right fragment otherwise if you have several fragments on the same level it would return to the first one if you don´t override the toolbar back button behavior.

    toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    
    0 讨论(0)
提交回复
热议问题