使用片段时在 Android Navigation Drawer 图像和 Up caret 之间切换 [英] Switching between Android Navigation Drawer image and Up caret when using fragments
问题描述
当使用 Navigation Drawer 时,Android 开发人员建议在 ActionBar 中只有那些在 Navigation Drawer 中表示的屏幕实际上应该具有 Navigation Drawer 图像"并且所有其他屏幕都具有传统的 up carat".
When using the Navigation Drawer the Android devs are recommending that in the ActionBar "only those screens that are represented in the Navigation Drawer should actually have the Navigation Drawer image" and that "all other screens have the traditional up carat."
请参阅此处了解详情:http://youtu.be/F5COhlbpIbY
我正在使用一个活动来控制多个级别的片段,并且可以让导航抽屉图像在所有级别上显示和运行.
I'm using one activity to control multiple levels of fragments and can get the Navigation Drawer image to display and function at all levels.
在创建较低级别的片段时,我可以调用 ActionBarDrawerToggle
setDrawerIndicatorEnabled(false)
来隐藏导航抽屉图像并显示向上插入符号
When creating lower level fragments I can call the ActionBarDrawerToggle
setDrawerIndicatorEnabled(false)
to hide the Navigation Drawer image and have the Up caret displayed
LowerLevelFragment lowFrag = new LowerLevelFragment();
//disable the toggle menu and show up carat
theDrawerToggle.setDrawerIndicatorEnabled(false);
getSupportFragmentManager().beginTransaction().replace(R.id.frag_layout,
lowFrag, "lowerFrag").addToBackStack(null).commit();
我遇到的问题是当我导航回顶级片段时,Up carat 仍然显示而不是原始的导航抽屉图像.关于如何刷新"顶级片段上的 ActionBar 以重新显示导航抽屉图像的任何建议?
The problem I'm having is when I navigate back to the top level fragments the Up carat still shows instead of the original Navigation Drawer image. Any suggestions on how to "refresh" the ActionBar on the top level fragments to re-display the Navigation Drawer image?
汤姆的建议对我有用.这是我所做的:
Tom's suggestion worked for me. Here’s what I did:
此活动控制应用中的所有片段.
This activity controls all fragments in the app.
在准备新片段以替换其他片段时,我将 DrawerToggle setDrawerIndicatorEnabled(false)
设置如下:
When preparing new fragments to replace others, I set the DrawerToggle setDrawerIndicatorEnabled(false)
like this:
LowerLevelFragment lowFrag = new LowerLevelFragment();
//disable the toggle menu and show up carat
theDrawerToggle.setDrawerIndicatorEnabled(false);
getSupportFragmentManager().beginTransaction().replace(R.id.frag_layout,
lowFrag).addToBackStack(null).commit();
接下来,在 onBackPressed
的覆盖中,我通过将 DrawerToggle 设置为 setDrawerIndicatorEnabled(true)
来恢复上述内容,如下所示:
Next, in an override of onBackPressed
, I reverted the above by setting the DrawerToggle to setDrawerIndicatorEnabled(true)
like this:
@Override
public void onBackPressed() {
super.onBackPressed();
// turn on the Navigation Drawer image;
// this is called in the LowerLevelFragments
setDrawerIndicatorEnabled(true)
}
<小时>
在LowerLevelFragments中
在片段中,我像这样修改了 onCreate
和 onOptionsItemSelected
:
在 onCreate
中添加了 setHasOptionsMenu(true)
以启用配置选项菜单.还要设置 setDisplayHomeAsUpEnabled(true)
以启用操作栏中的 <:
In onCreate
added setHasOptionsMenu(true)
to enable configuring the options menu. Also set setDisplayHomeAsUpEnabled(true)
to enable the < in the actionbar:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// needed to indicate that the fragment would
// like to add items to the Options Menu
setHasOptionsMenu(true);
// update the actionbar to show the up carat/affordance
getActivity().getActionBar().setDisplayHomeAsUpEnabled(true);
}
然后在 onOptionsItemSelected
中,每当按下 < 时,它都会从活动中调用 onBackPressed()
以在层次结构中向上移动一级并显示导航抽屉图像:
Then in onOptionsItemSelected
whenever the < is pressed it calls the onBackPressed()
from the activity to move up one level in the hierarchy and display the Navigation Drawer Image:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Get item selected and deal with it
switch (item.getItemId()) {
case android.R.id.home:
//called when the up affordance/carat in actionbar is pressed
getActivity().onBackPressed();
return true;
…
}
推荐答案
您已经写过,为了实现较低级别的片段,您正在替换现有的片段,而不是在新活动中实现较低级别的片段.
You have written that, to implement lower-level fragments, you are replacing the existing fragment, as opposed to implementing the lower-level fragment in a new activity.
我认为您必须手动实现返回功能:当用户按下返回时,您有弹出堆栈的代码(例如在 Activity::onBackPressed
覆盖中).因此,无论您在何处执行此操作,都可以反转 setDrawerIndicatorEnabled
.
I would think that you would then have to implement the back functionality manually: when the user pressed back you have code that pops the stack (e.g. in Activity::onBackPressed
override). So, wherever you do that, you can reverse the setDrawerIndicatorEnabled
.
这篇关于使用片段时在 Android Navigation Drawer 图像和 Up caret 之间切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!