调用onPrepareOptionsMenu时将触发SearchView.OnQueryTextListener方法onQueryTextChange [英] The SearchView.OnQueryTextListener method, onQueryTextChange, fires when onPrepareOptionsMenu is called
问题描述
在调用invalidateOptionsMenu()
之后调用onPrepareOptionsMenu
时,将触发SearchView.OnQueryTextListener
方法onQueryTextChange
.
The SearchView.OnQueryTextListener
method, onQueryTextChange
, fires when onPrepareOptionsMenu
is called after I invoke invalidateOptionsMenu()
.
我的SearchView OnQueryTextListener.
My SearchView OnQueryTextListener..
final private SearchView.OnQueryTextListener queryListener = new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextChange(String newText) {
loaderID = 1;
Bundle args = new Bundle();
args.putInt("loaderID", loaderID);
if(!TextUtils.isEmpty(newText)){
uri = Uri.parse(DataProvider.URIPREPEND + DataProvider.SEARCH + "/?" + newText);
Log.d("D", "onQueryTextChange() Search URI: " + uri.toString());
}
args.putParcelable("uri", uri);
try{
Log.d("D", "onQueryTextChange() case: search");
fragment.onMessage(args);
}catch (ClassCastException cce){
}
return false;
}
@Override
public boolean onQueryTextSubmit(String query) {
// Toast.makeText(getActivity(), "Searching for: " + query + "...", Toast.LENGTH_SHORT).show();
return false;
}
};
每当我从onOptionsItemSelected()内部调用invalidateOptionsMenu()时,它就会被调用.
It gets called every time I invoke invalidateOptionsMenu() from within onOptionsItemSelected()..
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Bundle args;
switch (item.getItemId()) {
case android.R.id.home:
uri = Uri.parse(DataProvider.URIPREPEND + DataProvider.BOOKMARKS);
loaderID = 0;
args = new Bundle();
args.putParcelable("uri", uri);
args.putInt("loaderID", loaderID);
try{
Log.d("D", "onOptionsItemSelected() case: home");
fragment.onMessage(args);
invalidateOptionsMenu();
}catch (ClassCastException cce){
}
return true;
default:
return super.onOptionsItemSelected(item);
}
}
据我了解,我管理onCreateOptionsMenu()
中的SearchView
,该一次被称为一次.我很困惑onPrepareOptionsMenu()
如何使我的textListener中的onQueryTextChange
被调用.
I manage the SearchView
in onCreateOptionsMenu()
which is called once, to my understanding. I am confused how onPrepareOptionsMenu()
could make the onQueryTextChange
in my textListener to get called.
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.searchactionbaritem).getActionView();
//searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setOnQueryTextListener(queryListener);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onPrepareOptionsMenu(Menu menu){
menu.setGroupEnabled(R.id.browse_group, true);
menu.setGroupVisible(R.id.browse_group, true);
menu.setGroupEnabled(R.id.recipe_group, false);
menu.setGroupVisible(R.id.recipe_group, false);
// home
if (loaderID == 0) {
Log.d("D", "mListFragment/onPrepareOptionsMenu() home");
menu.findItem(android.R.id.home).setEnabled(false).setIcon(R.drawable.ic_home_black_48dp);
menu.findItem(R.id.browse).setEnabled(true).setIcon(R.drawable.ic_browse_all_white_48dp);
} else { // browse_all or search
if(uri.getQuery() != null){ // search
Log.d("D", "mListFragment/onPrepareOptionsMenu() search");
menu.findItem(android.R.id.home).setEnabled(true).setIcon(R.drawable.ic_home_white_48dp);
menu.findItem(R.id.browse).setEnabled(true).setIcon(R.drawable.ic_browse_all_white_48dp);
}else{ // browse_all
Log.d("D", "mListFragment/onPrepareOptionsMenu() browse_all");
menu.findItem(android.R.id.home).setEnabled(true).setIcon(R.drawable.ic_home_white_48dp);
menu.findItem(R.id.browse).setEnabled(false).setIcon(R.drawable.ic_browse_all_black_48dp);
}
}
return super.onPrepareOptionsMenu(menu);
}
如果我删除了invalidateOptionsMenu()
,则我的搜索小部件将按预期工作.当它在那里时,即使我根本没有单击搜索小部件,我的搜索文本侦听器也会被调用.
If I remove the invalidateOptionsMenu()
then my search widget works as expected. When it is there, my search text listener gets called even when I haven't clicked on the search widget at all.
推荐答案
我删除了...
SearchView searchView = (SearchView) menu.findItem(R.id.searchactionbaritem).getActionView();
searchView.setOnQueryTextListener(queryListener);
从onCreateOptionsMenu()
放到onPrepareOptionsMenu()
中,它又可以正常工作.
from the onCreateOptionsMenu()
and put it in the onPrepareOptionsMenu()
and it is working normally again.
我希望这对其他人有帮助.我认为这与invalidateOptionsMenu()
破坏SearchView与actionview的关联有关.
I hope this helps someone else. I think it has something to do with invalidateOptionsMenu()
breaking the SearchView's association with the actionview.
这篇关于调用onPrepareOptionsMenu时将触发SearchView.OnQueryTextListener方法onQueryTextChange的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!