在我的ActionBarActivity
中,我有以下代码:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.near_location_menu, menu);
MenuItem searchItem = menu.findItem(R.id.action_user_search);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
setupNewSearchView(searchItem, searchManager);
return true;
}
private void setupNewSearchView(final MenuItem searchItem, SearchManager searchManager) {
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE) {
searchView.setIconifiedByDefault(false);
} else {
searchView.setIconifiedByDefault(true);
}
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
public boolean onQueryTextChange(String arg0) {
return false;
}
public boolean onQueryTextSubmit(String arg0) {
if (searchItem != null) {
searchItem.collapseActionView();
}
return false;
}
});
}
现在,SearchView
应该只在我转到特定的Fragment
时显示,在该Fragment
中,我这样做:
@Override
public void onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
menu.findItem(R.id.action_user_search).setVisible(true);
menu.findItem(R.id.action_view_spots).setVisible(false);
menu.findItem(R.id.action_refresh).setVisible(false);
getActivity().supportInvalidateOptionsMenu();
}
问题:
当我转到这个Fragment
,SearchView
时,确实显示了,但触摸没有发生任何变化。它不会打开或响应。我做错了什么?
补充信息:
ActionBarActivity
:
import android.support.v7.widget.SearchView;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
Fragment
:
import android.view.Menu;
import android.support.v4.app.ListFragment;
您在onCreateView
上设置了setHasOptionsMenu(true)
吗?