在我的应用程序(minSdkVersion
15)中,我有一个片段之间的Toolbar
代替ActionBar
和NavigationDrawer
切换。有些片段TabBar
里面有子片段。这些子片段是ListView
s 和它们的onItemClickListener
触发器DetailFragment
。我打电话setDisplayHomeAsUpEnabled()
并出现一个向上箭头,DetailFragment
但我不能让它执行任何操作,甚至显示敬酒。我已经尝试过并处理switch (item.getItemId())
过,onOptionsItemSelected()
但两种解决方案都不适合我。我错过了一些东西,但不知道是什么。
这是我的代码:
public class MainActivity extends AppCompatActivity implements FragmentManager.OnBackStackChangedListener {
//...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Listen for changes in the back stack
getSupportFragmentManager().addOnBackStackChangedListener(this);
//Handle when activity is recreated like on orientation Change
shouldDisplayHomeUp();
mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
final ActionBar actionBar = getSupportActionBar(); //...
...
public void shouldDisplayHomeUp(){
//Enable Up button only if there are entries in the back stack
boolean canback = getSupportFragmentManager().getBackStackEntryCount()>0;
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(canback);
actionBarDrawerToggle.setDrawerIndicatorEnabled(!canback);
}
}
@Override
public boolean onNavigateUp() {
//This method is called when the up button is pressed. Just the pop back stack.
getSupportFragmentManager().popBackStack();
return true;
}
@Override
public void onBackStackChanged() {
shouldDisplayHomeUp();
}
我的整个onOptionsItemSelected
:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
switch (item.getItemId()) {
case android.R.id.home:
Toast.makeText(this, "Up button!", Toast.LENGTH_SHORT).show();
//called when the up affordance/carat in actionbar is pressed
onBackPressed();
break;
case R.id.action_search:
Toast.makeText(this, "Search", Toast.LENGTH_SHORT).show();
break;
}
return super.onOptionsItemSelected(item);
}
我不知道其他代码是否重要:
// Initializing Drawer Layout and ActionBarToggle
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer);
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
actionBarDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,mToolbar,R.string.openDrawer, R.string.closeDrawer){//...
//Setting the actionbarToggle to drawer layout
mDrawerLayout.setDrawerListener(actionBarDrawerToggle);
//calling sync state is necessay or else your hamburger icon wont show up
actionBarDrawerToggle.syncState();
在我的清单中,我没有父活动。
我发现了这一点,这似乎是正确的答案,但我不明白如何实现它,也没有声望去问作者。
我可以尝试什么让向上按钮工作?
两个新问题:当我设置
public void shouldDisplayHomeUp(){
//Enable Up button only if there are entries in the back stack
if (getSupportActionBar() != null) {
if (getSupportFragmentManager().getBackStackEntryCount()>0) {
actionBarDrawerToggle.setDrawerIndicatorEnabled(false);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
} else {
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
actionBarDrawerToggle.setDrawerIndicatorEnabled(true);
}
}
}
...从子片段中,我按下硬件“返回”按钮,我返回到前一个片段,汉堡图标出现。但是向上按钮仍然没有来自子片段的响应。我相信这是因为ActionBarDrawerToggle
不再管理它的行为。但是谁来管理它呢?如果我这样设置这个方法:
public void shouldDisplayHomeUp(){
//Enable Up button only if there are entries in the back stack
if (getSupportActionBar() != null) {
if (getSupportFragmentManager().getBackStackEntryCount()>0) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
} else {
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
}
}
}
...然后ActionBarDrawerToggle
处理单击向上按钮并像汉堡图标一样打开抽屉。但是当我按下硬件后退按钮时,向上按钮(箭头)消失并且汉堡图标没有出现。
所以现在我看到了两种解决这个问题的方法。首先,我可以弄清楚谁在何时管理向上按钮
actionBarDrawerToggle.setDrawerIndicatorEnabled(false);
和
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
ActionBarDrawerToggle
否则,当向上按钮箭头可用而不是汉堡包图标时,如何覆盖该行为以执行另一个操作?当我按下硬件返回按钮时,如何使汉堡图标出现?