我有一个应用程序,它使用抽屉式导航来显示具有主要主题的不同片段。 Drawer navigation
在“首页”片段中,我显示不同的类别(那些彩色框),当我选择一个类别时,它会显示带有该类别的每日和常规详细信息的新片段。(称为“详细信息”片段)Home fragment
我想要并且不能弄清楚如何完成操作,是在那个Detail片段中,我希望有一个向后箭头,它将我带回到Home片段,而不是打开抽屉导航的汉堡菜单图标。 Detail fragment
答案 0 :(得分:0)
您可以通过动态地将工具栏添加到主要活动中来实现此目的,而不是在内置的导航抽屉中使用,而是从头开始。添加片段时,在片段上添加工具栏。 工具栏myChildToolbar = (工具栏)findViewById(R.id.my_child_toolbar); setSupportActionBar(myChildToolbar);
// Get a support ActionBar corresponding to this toolbar
ActionBar ab = getSupportActionBar();
// Enable the Up button
ab.setDisplayHomeAsUpEnabled(true);
然后,您将可以覆盖Button向上导航方法中的方法,以在工具栏上启用所需的后退按钮。
答案 1 :(得分:0)
如果我假设您在布局中使用android.support.v4.widget.DrawerLayout,则此方法可能对您有用;我仅在API 21上进行了测试,但是鉴于它主要使用支持库,因此它应该在较低或较高的目标上都可以工作(著名的最后话)。
导入android.support.v7.app.ActionBarDrawerToggle 导入android.support.v4.widget.DrawerLayout
ActionBarDrawerToggle mDrawerToggle;
DrawerLayout drawerLayout;
private boolean mToolBarNavigationListenerIsRegistered = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
// Get DrawerLayout ref from layout
drawerLayout = (DrawerLayout)findViewById(R.id.drawer);
// Initialize ActionBarDrawerToggle, which will control toggle of hamburger.
// You set the values of R.string.open and R.string.close accordingly.
// Also, you can implement drawer toggle listener if you want.
mDrawerToggle = new ActionBarDrawerToggle (this, drawerLayout, mToolbar, R.string.open, R.string.close);
// Setting the actionbarToggle to drawer layout
drawerLayout.setDrawerListener(mDrawerToggle);
// Calling sync state is necessary to show your hamburger icon...
// or so I hear. Doesn't hurt including it even if you find it works
// without it on your test device(s)
mDrawerToggle.syncState();
}
/**
* To be semantically or contextually correct, maybe change the name
* and signature of this function to something like:
*
* private void showBackButton(boolean show)
* Just a suggestion.
*/
private void enableViews(boolean enable) {
// To keep states of ActionBar and ActionBarDrawerToggle synchronized,
// when you enable on one, you disable on the other.
// And as you may notice, the order for this operation is disable first, then enable - VERY VERY IMPORTANT.
if(enable) {
//You may not want to open the drawer on swipe from the left in this case
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
// Remove hamburger
mDrawerToggle.setDrawerIndicatorEnabled(false);
// Show back button
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// when DrawerToggle is disabled i.e. setDrawerIndicatorEnabled(false), navigation icon
// clicks are disabled i.e. the UP button will not work.
// We need to add a listener, as in below, so DrawerToggle will forward
// click events to this listener.
if(!mToolBarNavigationListenerIsRegistered) {
mDrawerToggle.setToolbarNavigationClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Doesn't have to be onBackPressed
onBackPressed();
}
});
mToolBarNavigationListenerIsRegistered = true;
}
} else {
//You must regain the power of swipe for the drawer.
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
// Remove back button
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
// Show hamburger
mDrawerToggle.setDrawerIndicatorEnabled(true);
// Remove the/any drawer toggle listener
mDrawerToggle.setToolbarNavigationClickListener(null);
mToolBarNavigationListenerIsRegistered = false;
}
// So, one may think "Hmm why not simplify to:
// .....
// getSupportActionBar().setDisplayHomeAsUpEnabled(enable);
// mDrawer.setDrawerIndicatorEnabled(!enable);
// ......
// To re-iterate, the order in which you enable and disable views IS important #dontSimplify.
}
该解决方案使用ActionBarDrawerToggle.setDrawerIndicatorEnabled切换汉堡包图标的可见性,并使用ActionBar.setDisplayHomeAsUpEnabled切换“向上”按钮的可见性,基本上利用了它们各自的可绘制资源。
答案 2 :(得分:0)
感谢您的帮助,我从您的答案中获得了一些想法,并在Stackoverflow中获得了其他一些帖子。如果您认为这是不好的解决方案,请告诉我。 这就是我能够通过非常简单的解决方案(科特琳)使其工作的方式:
将其放在您的活动中onCreate
supportFragmentManager.addOnBackStackChangedListener {
actionBarDrawerToggle?.isDrawerIndicatorEnabled = supportFragmentManager.backStackEntryCount <= 0
}
并重写onSupportNavigateUp():
override fun onSupportNavigateUp(): Boolean {
supportFragmentManager.popBackStack()
return super.onSupportNavigateUp()
}