解决方案:
问题是由app:showAsAction="collapseActionView|always"
的使用造成的。用app:showAsAction="always"
替换它解决了这个问题。
我有ViewPager的片段,ViewPager有3个片段。每个片段在其菜单项中都有一个SearchView(在工具栏中显示为菜单项)。当我点击SearchView时,我在onOptionsItemSelected中获得了click事件,但SearchView没有扩展。不知道该怎么做。
我在setHasOptionMenu(true)
中为所有片段设置了onCreate()
。
这是我当前的代码:for each Fragment
onCreateOptionsMenu:
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu_new, menu);
SearchManager searchManager = (SearchManager) activity.
getSystemService(Context.SEARCH_SERVICE);
searchMenuItem = menu.findItem(R.id.ic_action_search);
searchView = (SearchView) searchMenuItem.getActionView();
searchView.setSearchableInfo(searchManager.
getSearchableInfo(activity.getComponentName()));
searchView.setOnQueryTextListener(this);
}
菜单XML文件:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/ic_action_search"
android:icon="@drawable/ic_search_white_24dp"
android:orderInCategory="0"
android:title="Search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="collapseActionView|always" />
<item
android:id="@+id/action_list"
android:icon="@drawable/ic_action_list_white"
android:orderInCategory="1"
android:title="@string/action_list"
android:checked="true"
android:checkable="true"
app:showAsAction="never" />
<item
android:id="@+id/action_grid"
android:icon="@drawable/ic_action_grid_white"
android:orderInCategory="2"
android:title="@string/action_grid"
android:checked="false"
android:checkable="true"
app:showAsAction="never" />
<item
android:id="@+id/action_mini"
android:icon="@drawable/ic_action_grid_white"
android:orderInCategory="3"
android:title="@string/action_mini"
android:checked="false"
android:checkable="true"
app:showAsAction="never" />
</menu>
其他相关代码:如果有帮助
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
mViewPagerPetitionsRecyclerViewAdapter.getFilter().filter(newText);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.action_list) {
prefs.edit().putString(Const.VIEWPAGER.VIEW_TYPE, Const.VIEWPAGER.LIST_VIEW).apply();
mStaggeredLayoutManager.setSpanCount(1);
} else if (item.getItemId() == R.id.action_grid) {
prefs.edit().putString(Const.VIEWPAGER.VIEW_TYPE, Const.VIEWPAGER.GRID_VIEW).apply();
mStaggeredLayoutManager.setSpanCount(2);
} else if (item.getItemId() == R.id.action_mini) {
prefs.edit().putString(Const.VIEWPAGER.VIEW_TYPE, Const.VIEWPAGER.MINI_VIEW).apply();
mStaggeredLayoutManager.setSpanCount(1);
}
ActivityCompat.invalidateOptionsMenu(activity);
mViewPagerPetitionsRecyclerViewAdapter.notifyDataSetChanged();
mIMainNewPetitionsListener.onLayoutChangedListener();
return super.onOptionsItemSelected(item);
}
@Override
public void onPrepareOptionsMenu(Menu menu) {
MenuItem list_item = menu.findItem(R.id.action_list);
MenuItem grid_item = menu.findItem(R.id.action_grid);
MenuItem mini_item = menu.findItem(R.id.action_mini);
String item_type = prefs.getString(Const.VIEWPAGER.VIEW_TYPE, Const.VIEWPAGER.LIST_VIEW);
if (item_type.equalsIgnoreCase(Const.VIEWPAGER.LIST_VIEW)) {
list_item.setChecked(true);
grid_item.setChecked(false);
mini_item.setChecked(false);
} else if (item_type.equalsIgnoreCase(Const.VIEWPAGER.GRID_VIEW)) {
list_item.setChecked(false);
grid_item.setChecked(true);
mini_item.setChecked(false);
} else if (item_type.equalsIgnoreCase(Const.VIEWPAGER.MINI_VIEW)) {
list_item.setChecked(false);
grid_item.setChecked(false);
mini_item.setChecked(true);
}
super.onPrepareOptionsMenu(menu);
}
答案 0 :(得分:0)
我认为你必须自己扩展行动观点:
在onOptionsItemSelected
:
if (item.getItemId() == R.id.action_search) {
item.expandActionVew();
}
答案 1 :(得分:0)