[英]Hide Menu item in a Fragment on button click Android
I have a fragment where I am displaying a Menu with 2 menu items. 我有一个片段,其中显示了包含2个菜单项的菜单。 Currently the menu items are shown as the fragment is displayed.
当前,菜单项显示为片段显示。 Now, I want to hide them once a certain action takes place eg.
现在,我想在执行某些操作后将它们隐藏起来。 User deletes some text.
用户删除一些文本。 I have tried the following code but the menu items still display :
我已经尝试了以下代码,但是菜单项仍然显示:
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
{
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu_details, menu);
}
@Override
public void onPrepareOptionsMenu(Menu menu)
{
if (isDeleted)
{
status = false;
menu.findItem(R.id.action_edit).setVisible(false);
}
}
}
How can I hide and unhide the menu items inside a fragment? 如何隐藏和取消隐藏片段中的菜单项?
What you've got now seems good, you're just missing invalidateOptionsMenu call: 您现在所拥有的似乎很好,只是缺少了invalidateOptionsMenu调用:
isDeleted = true;
getActivity().invalidateOptionsMenu();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.