简体   繁体   中英

Navigation drawer hamburger icon not showing up with v7 ActionBarDrawerToggle

I've been trying to create a navigation drawer for an existing app. I've found a few tutorials for this, but most of them (including the official Android guide ) appear to be for the v4 ActionBarDrawerToggle library, which has been deprecated. I'm trying to use the v7 library instead, but my ActionBarDrawerToggle doesn't seem to do what the documentation says it should do.

Edit: Modified my code as per the answer below. The hamburger icon now switches back and forth correctly, but when the user taps the hardware back button to go back to the main fragment of my app, the hamburger icon disappears entirely. Why does this happen?

private void addDrawerItems() {
    String[] itemArray = {"About", "Nearby", "Settings", "Feedback",};
    mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, itemArray);
    mDrawerList.setAdapter(mAdapter);
    mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Log.d("ContributionsActivity", "Item " + position + " selected");
        }
    });
}

private void setupDrawer() {
    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.drawer_open, R.string.drawer_close) {

        /** Called when a drawer has settled in a completely open state. */
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
            invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
        }

        /** Called when a drawer has settled in a completely closed state. */
        public void onDrawerClosed(View view) {
            super.onDrawerClosed(view);
            invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
        }
    };

    mDrawerToggle.setDrawerIndicatorEnabled(true);
    mDrawerLayout.setDrawerListener(mDrawerToggle);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setTitle(R.string.title_activity_contributions);
    setContentView(R.layout.activity_contributions);

    //Set up navigation drawer
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);
    mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
    mDrawerList = (ListView)findViewById(R.id.drawer_list);
    addDrawerItems();
    setupDrawer();
    ...
    }

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // enabling drawer toggle by clicking on the app icon.
    if (mDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    } else {
        switch (item.getItemId()) {
            case android.R.id.home:
                if (mediaDetails.isVisible()) {
                    getSupportFragmentManager().popBackStack();
                }
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
}

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

You're missing sync state, add it and everything should be fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM