演示此错误的项目:https ://github.com/smarek/ActionBar-Navigation-Bug
b.android.com 上的错误报告:http ://code.google.com/p/android/issues/detail?id =51449
我目前正面临 ActionBar 的问题。
让我们有一个 ViewPager+PagerTitleStrip 和 3 个片段。
用户流:
- 打开应用程序
- 第一个片段已将导航模式设置为 NAVIGATION_MODE_LIST
- 其他片段有 NAVIGATION_MODE_STANDARD
- 所有片段都有带有 SearchView 的选项菜单项
- 转到第二个或第三个并打开搜索(单击搜索项)
- 是否关闭搜索视图(您可以滑动回上一个片段)
- 转到第一个片段,看看,没有 NAVIGATION_MODE_LIST
- 在第一个片段打开搜索并关闭它
- 导航列表再次可见
只添加MainActivity的代码和布局项目是标准的Android Application Project,minSdk 11,创建主Activity时,选择空白Activity和滑动视图+标题条
布局/activity_main.xml
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<!--
This title strip will display the currently visible page title, as well as the page
titles for adjacent pages.
-->
<android.support.v4.view.PagerTitleStrip
android:id="@+id/pager_title_strip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:background="#33b5e5"
android:paddingBottom="4dp"
android:paddingTop="4dp"
android:textColor="#fff" />
</android.support.v4.view.ViewPager>
com/example/project/MainActivity.java
// imports ommited
public class MainActivity extends FragmentActivity {
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSectionsPagerAdapter = new SectionsPagerAdapter(
getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
}
public static class DummySectionFragment extends Fragment {
public static final String ARG_SECTION_NUMBER = "section_number";
public static final int MENU_SEARCH = -1;
protected MenuItem searchItem;
protected SearchView mSearchView;
public DummySectionFragment() {
setHasOptionsMenu(true);
}
/*
* Initializing menu items, adding only searchItem (aka SearchView in actionview)
*/
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
mSearchView = new SearchView(getActivity().getActionBar()
.getThemedContext());
searchItem = menu
.add(Menu.NONE, MENU_SEARCH, Menu.NONE, "Search")
.setIcon(android.R.drawable.ic_menu_search)
.setActionView(mSearchView)
.setShowAsActionFlags(
MenuItem.SHOW_AS_ACTION_ALWAYS
| MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
super.onCreateOptionsMenu(menu, inflater);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
TextView textView = new TextView(getActivity());
textView.setGravity(Gravity.CENTER);
textView.setText(Integer.toString(getArguments().getInt(
ARG_SECTION_NUMBER)));
return textView;
}
// Using setUserVisibleHint to operate with actionbar
// (navigation mode) and visibility of option menu items
// if isVisibleToUser, we're doing setup for current Fragment
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser) {
// setting navigation mode according to fragment
ActionBar ab = getActivity().getActionBar();
int mode = 0;
// ARG_SECTION_NUMBER is argument with numbers 1, 2, 3
switch (getArguments().getInt(ARG_SECTION_NUMBER)) {
default:
case 1:
mode = ActionBar.NAVIGATION_MODE_LIST;
// Simple adapter added to spinner, to be visible
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(
getActivity(),
android.R.layout.simple_spinner_dropdown_item,
new String[] { "A", "B", "C" });
ab.setListNavigationCallbacks(spinnerArrayAdapter,
new OnNavigationListener() {
@Override
public boolean onNavigationItemSelected(
int itemPosition, long itemId) {
return false;
}
});
break;
case 2:
case 3:
mode = ActionBar.NAVIGATION_MODE_STANDARD;
break;
}
getActivity().getActionBar().setNavigationMode(mode);
} else {
// resetting navigation mode
if (getActivity() != null
&& getActivity().getActionBar() != null)
getActivity().getActionBar().setNavigationMode(
ActionBar.NAVIGATION_MODE_STANDARD);
}
}
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public int getCount() {
return 3;
}
@Override
public Fragment getItem(int position) {
Fragment fragment = new DummySectionFragment();
Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
fragment.setArguments(args);
return fragment;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return getString(R.string.title_section1).toUpperCase();
case 1:
return getString(R.string.title_section2).toUpperCase();
case 2:
return getString(R.string.title_section3).toUpperCase();
}
return null;
}
}
}