简体   繁体   English

导航抽屉汉堡包图标未随v7 ActionBarDrawerToggle一起显示

[英]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. 我已经找到了一些有关此的教程,但是其中大多数教程(包括官方的Android指南 )似乎都是针对v4 ActionBarDrawerToggle库的,该库已被弃用。 I'm trying to use the v7 library instead, but my ActionBarDrawerToggle doesn't seem to do what the documentation says it should do. 我正尝试使用v7库,但是我的ActionBarDrawerToggle似乎并没有执行文档中规定的操作。

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. 您缺少同步状态,请添加它,一切都应该正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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