ActionBarDrawerToggle with AppCompatActivity and Toolbar Bac



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

如何捕获"后退"按钮&lt-Toolbar点击?


更新:

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

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

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

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

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

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

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

删除此行

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

相关内容

  • 没有找到相关文章

最新更新