您好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。