我正在拿一个带汉堡包的抽屉,当我打开一个片段时我想要移除汉堡包并用一个叫onBackPressed()
的箭头替换它。
正在发生的事情是汉堡被箭头取代,但箭头上的聆听者却没有被召唤。如果我完全删除ActionBarDrawerToggle
它将起作用。我该如何解决这个问题?
打开新片段:
Fragment_Subjects frag = new Fragment_Subjects();
fm.beginTransaction()
.replace(R.id.mainContainer, frag)
.addToBackStack(null)
.commit();
toggle.setDrawerIndicatorEnabled(false);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
设置抽屉切换:
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
toggle = new ActionBarDrawerToggle(this, drawer, mToolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); //back arrow works when this line is removed
assert drawer != null;
drawer.setDrawerListener(toggle);
toggle.syncState();
后退箭头听众:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
onBackPressed:
@Override
public void onBackPressed() {
if(drawer.isDrawerOpen(Gravity.START)){
drawer.closeDrawer(Gravity.START);
}else{
super.onBackPressed();
}
}
答案 0 :(得分:1)
试试这个:
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onBackPressed();
}
});