Drawer/Back ActionBar 中的按钮在返回时不可点击
Drawer/Back button in ActionBar not clickable when in back
我使用了我在此处找到的解决方案,当后退堆栈中没有任何内容时,操作栏上的左按钮用作抽屉切换,而当有时返回按钮。这是实现
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
ButterKnife.bind(this);
setSupportActionBar(mToolbar);
final ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
}
navigateTo(R.id.menu_drawer);
initDrawer();
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, mToolbar,
R.string.main_open_menu, R.string.main_close_menu)
{
public void onDrawerClosed(View view){
syncActionBarState();
}
public void onDrawerOpened(View view){
mDrawerToggle.setDrawerIndicatorEnabled(true);
}
};
mOnBackStackChangedListener =
new android.support.v4.app.FragmentManager.OnBackStackChangedListener() {
@Override
public void onBackStackChanged() {
syncActionBarState();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
getSupportFragmentManager().addOnBackStackChangedListener(mOnBackStackChangedListener);
}
private void syncActionBarState(){
int backStackCount = getSupportFragmentManager().getBackStackEntryCount();
mDrawerToggle.setDrawerIndicatorEnabled(backStackCount == 0);
if(backStackCount == 0){
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
}else{
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
}
}
作为抽屉切换按钮工作正常。当用作后退按钮时,它甚至不会触发 OnOptionsItemClickListener。我错过了什么?
您已将 setDisplayHomeAsUpEnabled()
方法实现为
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
所以你可以让它像 described here 一样工作,这是一种简单的方法
我使用了我在此处找到的解决方案,当后退堆栈中没有任何内容时,操作栏上的左按钮用作抽屉切换,而当有时返回按钮。这是实现
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
ButterKnife.bind(this);
setSupportActionBar(mToolbar);
final ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
}
navigateTo(R.id.menu_drawer);
initDrawer();
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, mToolbar,
R.string.main_open_menu, R.string.main_close_menu)
{
public void onDrawerClosed(View view){
syncActionBarState();
}
public void onDrawerOpened(View view){
mDrawerToggle.setDrawerIndicatorEnabled(true);
}
};
mOnBackStackChangedListener =
new android.support.v4.app.FragmentManager.OnBackStackChangedListener() {
@Override
public void onBackStackChanged() {
syncActionBarState();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
getSupportFragmentManager().addOnBackStackChangedListener(mOnBackStackChangedListener);
}
private void syncActionBarState(){
int backStackCount = getSupportFragmentManager().getBackStackEntryCount();
mDrawerToggle.setDrawerIndicatorEnabled(backStackCount == 0);
if(backStackCount == 0){
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
}else{
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
}
}
作为抽屉切换按钮工作正常。当用作后退按钮时,它甚至不会触发 OnOptionsItemClickListener。我错过了什么?
您已将 setDisplayHomeAsUpEnabled()
方法实现为
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
所以你可以让它像 described here 一样工作,这是一种简单的方法