您好Stackoverflow小组,
以下是我的搜索视图代码:
private void setupSearchView(MenuItem searchItem) {
MenuItemCompat.setOnActionExpandListener(searchItem, new MenuItemCompat.OnActionExpandListener() {
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
searchQuery = null;
updateData();
return true;
}
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
return true;
}
});
SearchManager searchManager = (SearchManager) (getSystemService(Context.SEARCH_SERVICE));
searchView = (SearchView) searchItem.getActionView();
if (searchView != null) {
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
searchView.clearFocus();
searchView.setOnQueryTextListener(onQueryTextListener);
}
}
setOnActionExpandListener和OnActionExpandListener已弃用,并且不起作用。有人可以帮我用代码修复它吗?
提前致谢
最佳答案
API级别26.1.0中已弃用此方法。
代替MenuItemCompat.OnActionExpandListener
直接使用MenuItem.OnActionExpandListener。
样本代码
MenuItem item = menu.findItem(R.id.action_order);
item.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
@Override
public boolean onMenuItemActionExpand(MenuItem menuItem) {
isSearch = true;
return true;
}
@Override
public boolean onMenuItemActionCollapse(MenuItem menuItem) {
isSearch = false;
return true;
}
});
您也可以获取帮助this。