Android 切换片段后如何还原操作栏 问题:
执行Android 切换片段后如何还原操作栏 问题:,android,android-fragments,android-actionbar,Android,Android Fragments,Android Actionbar,执行片段回退,以便在一个活动中向后遍历一堆片段时,不会将操作栏恢复到上一个片段中的原始状态 为什么会发生这种情况? 事实证明,操作栏实际上是附加到活动本身的,而不是片段!请记住,片段只是UI的模块位,必须明确指定对其他片段、活动部分甚至操作栏的控制 继续阅读解决方案…解决方案: 我发现解决这个问题的最佳方法是通过Reto Meier对前一个问题的描述。我的解决方案将在他的答案上更深入地展开 但我们要确定的是,我们不希望每次切换到不同的片段时都重新创建动作栏,因为这样做效率不高。我将带您浏览我为学
片段
回退,以便在一个活动中向后遍历一堆片段时,不会将操作栏恢复到上一个片段中的原始状态
为什么会发生这种情况?
事实证明,操作栏实际上是附加到活动本身的,而不是片段!请记住,片段只是UI的模块位,必须明确指定对其他片段、活动部分甚至操作栏的控制
继续阅读解决方案…解决方案:
我发现解决这个问题的最佳方法是通过Reto Meier对前一个问题的描述。我的解决方案将在他的答案上更深入地展开
但我们要确定的是,我们不希望每次切换到不同的片段时都重新创建动作栏,因为这样做效率不高。我将带您浏览我为学生日程安排应用程序编写的一个应用程序。它不是很复杂,它的入职体验是由一个活动中的多个片段组成的
为了实现这一点,我们需要确保使用replace()
在片段之间切换。这比将片段层叠在一起要好,因为它允许您为每个片段分别配置操作栏
第一块代码来自活动的内部类,LoginOptionsFragment
,在其onCreateView()方法中
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_login_options, container, false);
//LoginOptionsFragment will have its own action bar
setHasOptionsMenu(true);
//inject views. e.g: Button add_course
ButterKnife.inject(this, rootView);
add_course.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
getFragmentManager().beginTransaction()
//exchange fragments. no messy clean-up necessary.
.replace(R.id.container, new AddCourseFragment())
.addToBackStack(null)
.commit();
}
});
return rootView;
}
在这里,我不仅确保通过设置选项菜单(true)
调用onCreateOptions菜单()
,而且主要是在单击“添加课程”按钮切换到添加课程标签后,新片段替换旧片段作为活动的主要子级。接下来,在我们覆盖了onCreateOptions菜单()
之后,我们将进入onResume()
,但稍后我们将讨论这个问题;)
其次,我们到达了AddCourseFragment
,在这里,我们甚至为操作栏膨胀了一个自定义的done cancel视图。让我们看看代码
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// BEGIN_INCLUDE (inflate_set_custom_view)
// Inflate a "Done/Cancel" custom action bar view.
final ActionBar actionBar = getActivity().getActionBar();
inflater = (LayoutInflater) actionBar.getThemedContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
//inflate custom action bar view
View customActionBarView = inflater.inflate(
R.layout.actionbar_custom_view_done_cancel, null);
//set listeners to items in the view
customActionBarView.findViewById(R.id.actionbar_done).setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
// "Done"
//remove custom view from action bar
actionBar.setDisplayShowCustomEnabled(false);
getFragmentManager().popBackStack();
//add course to list
}
});
customActionBarView.findViewById(R.id.actionbar_cancel).setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
// "Cancel"
//remove custom view from action bar
actionBar.setDisplayShowCustomEnabled(false);
getFragmentManager().popBackStack();
}
});
// Show the custom action bar view and hide the normal Home icon and title.
actionBar.setDisplayOptions(
ActionBar.DISPLAY_SHOW_CUSTOM,
ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME
| ActionBar.DISPLAY_SHOW_TITLE);
actionBar.setCustomView(customActionBarView,
new ActionBar.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
actionBar.setDisplayHomeAsUpEnabled(false);
// END_INCLUDE (inflate_set_custom_view)
View rootView = inflater.inflate(R.layout.fragment_add_course, container, false);
ButterKnife.inject(this, rootView);
return rootView;
}
您需要注意的唯一部分是OnClickListener
添加到完成和取消按钮中。在这里,我使用我以前对父级活动的操作栏的引用,并告诉它停止显示自定义视图。现在,除了这个特定的方法之外,还有更多的setDisplayXEnabled()
方法可以传入false。在那之后,我弹出了backback以进入前一个片段
但是我实际上如何恢复操作栏呢!?
这是怎么做的。还记得在我们的登录操作片段中挂起的onResume()
方法吗?好的,onResume()
在片段从backbackback返回焦点时被调用!因此,如果我们覆盖它并重新启用我们想要的动作栏部分,我们就赢了,对吗?是的,我们有。以下是您需要添加到onResume()
中的所有内容
@覆盖
恢复时公开作废(){
super.onResume();
ActionBar ActionBar=getActivity().getActionBar();
actionBar.setDisplayShowHomeEnabled(true);//显示主页图标
actionBar.setDisplayShowTitleEnabled(true);//显示标题
//actionBar.setDisplayUseLogoEnabled(true);
@Override
public void onResume() {
super.onResume();
ActionBar actionBar = getActivity().getActionBar();
actionBar.setDisplayShowHomeEnabled(true); //show Home icon
actionBar.setDisplayShowTitleEnabled(true); //show title
// actionBar.setDisplayUseLogoEnabled(true); <--- more options
// actionBar.setDisplayHomeAsUpEnabled(true); <--- more options
}