问题
I'm using SearchView
while my activity is extanding Sherlock Library.
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// TODO Auto-generated method stub
inflater.inflate(R.menu.countriesxml, menu);
SearchView mSearchView = (SearchView) menu.findItem(R.id.searchall)
.getActionView();
mSearchView.setQueryHint("Search for smartphones");
MenuItem menuItem = menu.findItem(R.id.searchall);
menuItem.setOnActionExpandListener(new OnActionExpandListener() {
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
return false;
}
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
// TODO Auto-generated method stub
item.collapseActionView();
indicator.setVisibility(View.VISIBLE);
pager.setVisibility(View.VISIBLE);
lvth.setVisibility(View.GONE);
return true;
}
});
return;
}
Please note that i tried to use OnCloseListener() but it didn't work.
What's wrong?
回答1:
have a look at your code at this section:
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
return false;
}
Replace it with below code and try again.
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
return true;
}
回答2:
Try using OnFocusChangeListener instead
menuItem.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean queryTextFocused) {
if (!queryTextFocused) {
// Code here
}
}
});
来源:https://stackoverflow.com/questions/20869445/searchview-onmenuitemactioncollapse-not-working