我正在使用带有NavigationView的ActionBarDrawerToggle。我的内容使用片段显示。
我正在关注this stackoverflow question以按下后退按钮,但控件永远不会流向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();
}
}
如何捕捉后退按钮< - 点击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();
}
}
答案 0 :(得分:2)
如果您希望在单击切换时触发onOptionsItemSelected()
方法,请从toolbar
构造函数调用中删除ActionBarDrawerToggle
参数。
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.string.openDrawerContentDescRes, R.string.closeDrawerContentDescRes);
否则,切换器会在内部处理打开和关闭抽屉,并且无需调用ActionBarDrawerToggle#onOptionsItemSelected()
。
如果您希望根据当前状态以不同方式点击主页Button
,则您还要删除if
顶部返回的onOptionsItemSelected()
块方法。
而且,您应该在setDisplayHomeAsUpEnabled(true)
中拨打onCreate()
一次。你不需要继续打开和关闭它。启用和禁用抽屉指示器将解决这个问题。
答案 1 :(得分: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);
}
}