android searchview setOnActionExpandListener on Honeycomb 3.2
2022-09-04 05:07:14
我正在为Android 3.2及更高版本开发一个应用程序。我需要在操作栏中的 SearchView 展开和折叠时实现“拦截”。我的Android 4.0及更高版本的代码还可以,但对于3.2没有。android-support-v4
OnActionExpandListener
菜单.xml
<item android:id="@+id/menu_search"
android:title="@string/menu_search"
android:icon="@android:drawable/ic_menu_search"
android:showAsAction="collapseActionView|always"
android:actionViewClass="android.widget.SearchView" />
我的活动.java
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.reader, menu);
final MenuItem searchMI = menu.findItem(R.id.menu_search);
if(searchView == null) {
//searchView = (SearchView) searchMI.getActionView();
searchView = (SearchView) MenuItemCompat.getActionView(searchMI);
searchView.setOnQueryTextListener(this);
searchView.setOnSuggestionListener(this);
searchView.setOnCloseListener(new OnCloseListener() {
@Override
public boolean onClose() {
//some code
return false;
}
});
}
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion <= android.os.Build.VERSION_CODES.HONEYCOMB_MR2) {
MenuItemCompat.setShowAsAction(searchMI, MenuItemCompat.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW | MenuItemCompat.SHOW_AS_ACTION_ALWAYS);
MenuItemCompat.setOnActionExpandListener(searchMI, new OnActionExpandListener() {
/* (non-Javadoc)
* @see android.support.v4.view.MenuItemCompat.OnActionExpandListener#onMenuItemActionExpand(android.view.MenuItem)
*/
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
Toast.makeText(getApplicationContext(), "onMenuItemActionExpand", Toast.LENGTH_SHORT).show();
return true;
}
/* (non-Javadoc)
* @see android.support.v4.view.MenuItemCompat.OnActionExpandListener#onMenuItemActionCollapse(android.view.MenuItem)
*/
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
Toast.makeText(getApplicationContext(), "onMenuItemActionExpand", Toast.LENGTH_SHORT).show();
return true;
}
});
} else {
searchMI.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
Toast.makeText(getApplicationContext(), "MenuItem#onMenuItemActionExpand", Toast.LENGTH_SHORT).show();
return true;
}
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
Toast.makeText(getApplicationContext(), "MenuItem#onMenuItemActionExpand", Toast.LENGTH_SHORT).show();
return true;
}
});
}
}
为什么对于Honeycomb,不调用侦听器的方法?
非常感谢。