问题描述
我有一些代码可以设置布尔值(无论搜索视图是否打开).
I have some code that sets a boolean if the search view is open or not.
MenuItemCompat.setOnActionExpandListener(action_search, new MenuItemCompat.OnActionExpandListener() {
@Override
public boolean onMenuItemActionExpand(MenuItem item){
isSearch = true;
return true;
}
@Override
public boolean onMenuItemActionCollapse(MenuItem item){
isSearch = false;
return true;
}
});
有效.我对它的响应方式感到满意.但是,它显示为已弃用,setOnActionExpandListener被警告删除
It works. I'm happy with the way it responds. However, it it shows as deprecated, the setOnActionExpandListener is crossed out with the warning
建议?
推荐答案
是 MenuItemCompat.setOnActionExpandListener
.此方法已在API级别26.1.0中弃用.
Yes MenuItemCompat.setOnActionExpandListener
This method was deprecated in API level 26.1.0.
使用 MenuItem.setOnActionExpandListener(MenuItem.OnActionExpandListener)
直接.
- 展开或折叠相关动作
-
MenuItemCompat.OnActionExpandListener
.menu item
必须配置为使用标志SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW
展开或折叠其动作view
.
view
时将通知此菜单项上的MenuItemCompat.OnActionExpandListener
on this menu item to be notified when the associated actionview
is expanded or collapsed. Themenu item
must be configured to expand or collapse its actionview
using the flagSHOW_AS_ACTION_COLLAPSE_ACTION_VIEW
.
示例代码
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;
}
});
这篇关于不推荐使用MenuItemCompat.setOnActionExpandListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!