我的工具栏包含搜索图标和刷新图标,当我点击搜索图标然后从onCreateOptionMenu()搜索视图膨胀但刷新图标不会隐藏。如果我在onMenuItemActionExpand()中隐藏刷新图标,那么在背面按下搜索视图返回图标两个菜单项搜索和刷新获取隐藏和默认设置图标变得可见。下面是我已经实现的代码。请帮助我!!!
MenuItem.java
@Override
public boolean onCreateOptionsMenu(final Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
item_search = menu.findItem(R.id.action_search);
item_refresh = menu.findItem(R.id.action_refresh);
final SearchView searchView = (SearchView) MenuItemCompat.getActionView(item_search);
searchView.setOnSearchClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// hide action item
if (menu != null) {
menu.findItem(R.id.action_refresh).setVisible(false);
}
}
});
searchView.setOnCloseListener(new SearchView.OnCloseListener() {
@Override
public boolean onClose() {
// re-show the action button
if (menu != null) {
menu.findItem(R.id.action_refresh).setVisible(true);
}
return false;
}
});
答案 0 :(得分:0)
MenuItem action_search;
MenuItem action_refresh;
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
action_search = menu.findItem(R.id. action_search);
action_refresh = menu.findItem(R.id. action_refresh);
return super.onPrepareOptionsMenu(menu);
}
@Override
public boolean onCreateOptionsMenu(final Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
item_search = menu.findItem(R.id.action_search);
item_refresh = menu.findItem(R.id.action_refresh);
final SearchView searchView = (SearchView) MenuItemCompat.getActionView(item_search);
searchView.setOnSearchClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// hide action item
if (action_refresh != null) {
action_refresh.setVisible(false);
invalidateOptionsMenu();
}
}
});
searchView.setOnCloseListener(new SearchView.OnCloseListener() {
@Override
public boolean onClose() {
// re-show the action button
if (action_search != null) {
action_search.setVisible(true);
}
if (action_refresh != null) {
action_refresh.setVisible(true);
}
invalidateOptionsMenu();
return false;
}
});
答案 1 :(得分:0)
Well you could imitate that yourself by hiding all the other items when the SearchView is expanded:
@Override
public boolean onCreateOptionsMenu(final Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
final MenuItem searchItem = menu.findItem(R.id.search);
SearchView searchView = (android.widget.SearchView) searchItem.getActionView();
// Detect SearchView icon clicks
searchView.setOnSearchClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setItemsVisibility(menu, searchItem, false);
}
});
// Detect SearchView close
searchView.setOnCloseListener(new SearchView.OnCloseListener() {
@Override
public boolean onClose() {
setItemsVisibility(menu, searchItem, true);
return false;
}
});
return super.onCreateOptionsMenu(menu);
}
private void setItemsVisibility(Menu menu, MenuItem exception, boolean visible) {
for (int i=0; i<menu.size(); ++i) {
MenuItem item = menu.getItem(i);
if (item != exception) item.setVisible(visible);
}
}
答案 2 :(得分:0)
它似乎是Android的一个错误,你可以使用解决方案here!