[英]ActionBar BUG: List mode navigation not visible after using SearchView
Project demonstrating this bug: https://github.com/smarek/ActionBar-Navigation-Bug 项目演示了这个错误: https : //github.com/smarek/ActionBar-Navigation-Bug
Bugreport on b.android.com : http://code.google.com/p/android/issues/detail?id=51449 b.android.com上的Bugreport : http : //code.google.com/p/android/issues/detail? id = 51449
I'm currently facing an issue with ActionBar. 我目前正面临ActionBar的问题。
Let's have a ViewPager+PagerTitleStrip and 3 Fragments. 我们有一个ViewPager + PagerTitleStrip和3个片段。
User flow: 用户流程:
Adding just the code of MainActivity and layout the project is standard Android Application Project, minSdk 11, when creating main activity, select blank activity and swipe views + title strip 仅添加MainActivity的代码和布局项目是标准的Android应用程序项目minSdk 11,在创建主要活动时,选择空白活动并滑动视图+标题条
layout/activity_main.xml 布局/ 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 COM /例子/项目/ 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;
}
}
}
Bug 窃听器
I am posting this just to point the discussion in the direction of the bug. 我发布这个只是为了将讨论指向错误的方向。
The bug is more related to the SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW
flag. 该错误与
SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW
标志更相关。 Once you remove the flag, everything will work perfectly. 删除标志后,一切都会完美运行。 It is most probably a bug but once again I am not sure if there is a rational explanation.
这很可能是一个错误,但我再次不确定是否有一个合理的解释。
Solution (not really) 解决方案 (不是真的)
When you're creating the menu item in onCreateOptionsMenu
, remove the ORed MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW
在
onCreateOptionsMenu
创建菜单项时,删除ORed MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW
Simply, change this 简单地说,改变它
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);
to 至
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);
Bug without viewpager 没有viewpager的bug
I have forked the project and here it is without the viewpager with the same behaviour ActionBar-Navigation-Bug 我已经分叉了项目,这里没有viewpager具有相同的行为ActionBar-Navigation-Bug
I had a similar problem when the SearchView had the SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW flag set. 当SearchView设置了SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW标志时,我遇到了类似的问题。 My workaround was to expand and collapse the MenuItem of the SearchView after switching to the NAVIGATION_MODE_LIST:
我的解决方法是在切换到NAVIGATION_MODE_LIST之后展开和折叠SearchView的MenuItem:
actionBar.setNavigationMode( ActionBar.NAVIGATION_MODE_LIST );
actionBar.setDisplayShowTitleEnabled( false );
searchMenuItem.expandActionView();
searchMenuItem.collapseActionView();
It is kind of dirty but it did the job for me. 它有点脏,但它为我做了工作。
Btw... The same bug and solution also applies to ActionBarSherlock. 顺便说一句......同样的错误和解决方案也适用于ActionBarSherlock。
Here is how I got round this bug: 以下是我如何解决这个问题:
First I made the MenuItem global in the activity: 首先,我在活动中创建了MenuItem全局:
private MenuItem searchItem;
In the activity I had the following method: 在活动中,我有以下方法:
public void hideSearch()
{
MenuItemCompat.collapseActionView(searchItem);
}
Then in the onCreateView() method of the fragment I call this method, just before returning the view. 然后在片段的onCreateView()方法中,我在返回视图之前调用此方法。 My spinner then stopped being hidden.
我的旋转器然后停止隐藏。 To be honest, Im not sure why this works, but it did for me.
说实话,我不确定为什么会这样,但它确实适合我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.