1

我正在使用带有 NavigationView 的 ActionBarDrawerToggle。我的内容使用片段显示。

我正在关注这个 stackoverflow 问题,以使后退按钮按下工作,但控制永远不会流向onOptionsItemSelected.

这是我的 MainActivity.class:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.openDrawerContentDescRes, R.string.closeDrawerContentDescRes);
    mDrawerLayout.addDrawerListener(mDrawerToggle);
    mDrawerToggle.syncState();

@Override
public void onBackPressed() {
    if (getFragmentManager().getBackStackEntryCount() == 0) {
        super.onBackPressed();
    } else {
        removeFragmentFromBackstack();
        updateToolbarWithHomeButton();
    }

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (mDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }

    switch (item.getItemId()) {
        case android.R.id.home:
            // doesn't reach here ever.
            return true;
        case R.id.action_x:
            // do something
            return true;
        case R.id.action_y:
            // do something
            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
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    mDrawerToggle.onConfigurationChanged(newConfig);
}

private void updateToolbarWithBackButton() {
    ActionBar actionBar = getSupportActionBar();
    if (null != mDrawerToggle && null != actionBar) {
        mDrawerToggle.setDrawerIndicatorEnabled(false);
        actionBar.setDisplayHomeAsUpEnabled(true);
    }
}

private void updateToolbarWithHomeButton() {
    ActionBar actionBar = getSupportActionBar();
    if (null != mDrawerToggle && null != actionBar) {
        actionBar.setDisplayHomeAsUpEnabled(false);
        mDrawerToggle.setDrawerIndicatorEnabled(true);
        mDrawerToggle.syncState();
    }
}

如何捕获后退按钮<- click from Toolbar


更新:

感谢@mike,工具栏上的后退箭头按钮现在onOptionsItemSelected在我的 MainActivity 代码中被捕获,如下更新。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.openDrawerContentDescRes, R.string.closeDrawerContentDescRes);
    mDrawerLayout.addDrawerListener(mDrawerToggle);
    mDrawerToggle.syncState();

@Override
public void onBackPressed() {
    if (getFragmentManager().getBackStackEntryCount() == 0) {
        super.onBackPressed();
    } else {
        removeFragmentFromBackstack();
        updateToolbarWithHomeButton();
    }

}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            //TODO: skip back press if fragment backstack count is 0.
            onBackPressed();
            updateToolbarWithHomeButton();
            return true;
        case R.id.action_x:
            // do something
            return true;
        case R.id.action_y:
            // do something
            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
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    mDrawerToggle.onConfigurationChanged(newConfig);
}

private void updateToolbarWithBackButton() {
    ActionBar actionBar = getSupportActionBar();
    if (null != mDrawerToggle && null != actionBar) {
        mDrawerToggle.setDrawerIndicatorEnabled(false);
    }
}

private void updateToolbarWithHomeButton() {
    ActionBar actionBar = getSupportActionBar();
    if (null != mDrawerToggle && null != actionBar) {
        mDrawerToggle.setDrawerIndicatorEnabled(true);
        mDrawerToggle.syncState();
    }
}
4

2 回答 2

2

如果您希望onOptionsItemSelected()方法在单击切换时触发,请从构造函数调用中删除toolbar参数。ActionBarDrawerToggle

mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
    R.string.openDrawerContentDescRes, R.string.closeDrawerContentDescRes);

否则,切换开关会在内部处理打开和关闭抽屉,并且ActionBarDrawerToggle#onOptionsItemSelected()不需要调用 to。

如果您想Button根据当前状态以不同方式处理单击主页,您还需要删除if在方法顶部返回的块onOptionsItemSelected()

而且,您应该setDisplayHomeAsUpEnabled(true)只调用一次onCreate(). 你不需要一直打开和关闭它。启用和禁用抽屉指示器将解决这个问题。

于 2016-05-05T08:00:15.430 回答
0

删除此行

 if (mDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }

onOptionsItemSelected()所以它看起来像这样

@Override
public boolean onOptionsItemSelected(MenuItem item) {

switch (item.getItemId()) {
    case android.R.id.home:
        // doesn't reach here ever.
        return true;
    case R.id.action_x:
        // do something
        return true;
    case R.id.action_y:
        // do something
        return true;
    default:
        return super.onOptionsItemSelected(item);
}
}
于 2016-05-05T08:34:34.403 回答