Clicking hamburger icon on Toolbar does not open Navigation Drawer

后端 未结 13 2785
太阳男子
太阳男子 2020-12-14 00:08

I have a simple android.support.v7.widget.Toolbar and all I am trying to do is to open a NavigationDrawer by pressing the \"hamburger\" icon in the top left cor

相关标签:
13条回答
  • 2020-12-14 00:36

    You need to sync the drawer toggle:

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        // Sync the toggle state after onRestoreInstanceState has occurred.
        mDrawerToggle.syncState();
    }
    

    EDIT: That code is working for me (copied from your post)

    public class TempActivity extends AppCompatActivity {
        private ActionBarDrawerToggle mDrawerToggle;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.temp);
            setupDrawer();
        }
        @Override
        public void onConfigurationChanged(Configuration newConfig) {
            super.onConfigurationChanged(newConfig);
            mDrawerToggle.onConfigurationChanged(newConfig);
        }
        @Override
        public boolean onOptionsItemSelected(final MenuItem item) {
            if (mDrawerToggle.onOptionsItemSelected(item)) {
                return true;
            }
            return super.onOptionsItemSelected(item);
        }
        @Override
        protected void onPostCreate(Bundle savedInstanceState) {
            super.onPostCreate(savedInstanceState);
            mDrawerToggle.syncState();
        }
        private void setupDrawer() {
            Toolbar toolbar = (Toolbar) findViewById(R.id.my_toolbar);
            setSupportActionBar(toolbar);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setHomeButtonEnabled(true);
            DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.my_drawer_layout);
            mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,toolbar,R.string.drawer_open, R.string.drawer_close) {
                public void onDrawerClosed(View view) {
                    super.onDrawerClosed(view);
                    invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
                }
                public void onDrawerOpened(View drawerView) {
                    super.onDrawerOpened(drawerView);
                    invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
                }
            };
            mDrawerLayout.setDrawerListener(mDrawerToggle);
        }
    }
    
    <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/my_drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <android.support.v7.widget.Toolbar
                android:id="@+id/my_toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
            <FrameLayout
                android:id="@+id/content_frame"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
        </LinearLayout>
        <RelativeLayout
            android:id="@+id/left_drawer"
            android:layout_width="240dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:choiceMode="singleChoice"
            android:divider="@android:color/transparent"
            android:dividerHeight="0dp"
            android:background="#111">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="#FFF"
                android:text="DRAMER MENU" />
        </RelativeLayout>
    </android.support.v4.widget.DrawerLayout>
    

    But if you are using the new NavigationView, then you don't need the toggle etc. Here is a good example how to use it.

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

    Simply call function onOptionsItemSelected(MenuItem menuItem) of ActionBarDrawerToggle class on activity options menu managing function like below.

    mDrawerToggle= new ActionBarDrawerToggle(activity, drawerLayout, R.string.nav_drawer_accessbility_drawer_open,
                R.string.nav_drawer_accessbility_drawer_close);
    
    
     @Override
    public boolean onOptionsItemSelected(final android.view.MenuItem item) {
        navigation().getOptionsMenuInflater(this).closeSearchView();
        // The action bar home/up action should open or close the drawer.
        // ActionBarDrawerToggle will take care of this behavior.
        mDrawerToggle.onOptionsItemSelected(item);
    
        return super.onOptionsItemSelected(item);
    }
    

    OR

    override onOptionsItemSelected of activity like below

    public boolean onOptionsItemSelected(MenuItem item) {
        if (item != null && item.getItemId() == android.R.id.home) {
            toggle();
    
        }
        return super.onOptionsItemSelected(item);
    }
    
     private void toggle() {
        if (mDrawerLayout.isDrawerVisible(GravityCompat.START)) {
            mDrawerLayout.closeDrawer(GravityCompat.START);
        } else {
            mDrawerLayout.openDrawer(GravityCompat.START);
        }
    }
    
    0 讨论(0)
  • 2020-12-14 00:36

    I fixed it by giving openable which is drawerLayout to navigateUp function.

    More info navigateUp Doc

     override fun onSupportNavigateUp(): Boolean {
        val navController = this.findNavController(R.id.host_fragment)
        return NavigationUI.navigateUp(navController,drawer_layout)
    }
    
    0 讨论(0)
  • 2020-12-14 00:36

    If none of the above answer works, please cross check again,

    First you need to call setSupportActionBar(toolbar); and then do the necessary work for nav drawer. I wasted my 2 hours because of this silly mistake.

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

    Override onOptionsItemSelected method and use below

    if(item.getItemId() == android.R.id.home){ // use android.R.id
        mDrawerLayout.openDrawer(Gravity.LEFT);
    }
    
    0 讨论(0)
  • 2020-12-14 00:44

    As i have no specific answer to your problem, i want to suggest a completely different approach:

    In my projects im using the Material Drawer developed by Mike Penz. Its looks really nice, is easy to implement, and there is not much you have to worry about.

    So if you cant find a solution for your problem, you could give it a try.

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