导航抽屉汉堡包图标没有出现与v7 ActionBarDrawerToggle

时间:2016-08-17 08:26:13

标签: android navigation-drawer hamburger-menu appcompat-v7-r22.1

我一直在尝试为现有应用创建导航抽屉。我已经为此找到了一些教程,但其中大多数(包括official Android guide)似乎是针对v4 ActionBarDrawerToggle库的,已经弃用了该库。我尝试使用v7库,但我的ActionBarDrawerToggle似乎没有做documentation所说的应该做的事情。

编辑:根据以下答案修改我的代码。汉堡包图标现在可以正常来回切换,但当用户点击硬件返回按钮返回我的应用程序的主要片段时,汉堡包图标完全消失。为什么会这样?

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();
}

1 个答案:

答案 0 :(得分:1)

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

你错过了同步状态,添加它,一切都应该没问题。