Android 在某些片段中隐藏 MenuItem
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21498534/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hide MenuItem in some Fragments
提问by pavol.franek
I using menu drawer which has more Fragment
s. In some Fragment
s I have menu item REFRESH but in some fragments I want hide this menu item (I don't want show menu but I don't want hide ActionBar
).
我使用有更多Fragment
s 的菜单抽屉。在某些Fragment
s 我有菜单项 REFRESH 但在某些片段中我想隐藏这个菜单项(我不想显示菜单但我不想隐藏ActionBar
)。
I try add override onCreateOptionsMenu()
to Fragment
where I don't want show this menu item but I can not get it to work. I try many way see commented line in code. Does any idea where is problem? And last this menu item go to hide when I activate menu drawer when is called onPrepareOptionsMenu()
in MainActivity but I need do this when I'm in Fragment
.
我尝试将覆盖添加onCreateOptionsMenu()
到Fragment
我不想显示此菜单项的位置,但我无法让它工作。我尝试了很多方法来查看代码中的注释行。知道问题出在哪里吗?最后,当我onPrepareOptionsMenu()
在 MainActivity 中调用时激活菜单抽屉时,此菜单项会隐藏,但是当我在Fragment
.
Fragment
where I want hide menu item REFRESH:
Fragment
我想隐藏菜单项刷新的地方:
public class FindPeopleFragment extends Fragment {
public FindPeopleFragment(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_find_people, container, false);
//setHasOptionsMenu(false);
return rootView;
}
private Menu menu=null;
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
{
inflater.inflate(R.menu.main, menu);
this.menu=menu;
menu.findItem(R.id.refresh).setVisible(false);
getActivity().invalidateOptionsMenu();
//setHasOptionsMenu(false);
super.onCreateOptionsMenu(menu,inflater);
}
}
MainActivity where is defined MENU DRAWER:
MainActivity 在哪里定义了 MENU DRAWER:
//Slide menu item click listener
private class SlideMenuClickListener implements
ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// display view for selected nav drawer item
displayView(position);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// toggle nav drawer on selecting action bar app icon/title
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
switch (item.getItemId()) {
case R.id.refresh:
Toast.makeText(this, "Refreshing data...", Toast.LENGTH_SHORT).show();
return true;
}
return super.onOptionsItemSelected(item);
}
// Called when invalidateOptionsMenu() is triggered
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
// if nav drawer is opened, hide the action items
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
menu.findItem(R.id.refresh).setVisible(!drawerOpen);
return super.onPrepareOptionsMenu(menu);
}
采纳答案by Luis
In the Fragment
where you don't want to show any menu options, you need setHasOptionsMenu(false);
in the onCreate()
, like this:
在Fragment
您不想显示任何菜单选项的地方,您需要setHasOptionsMenu(false);
在 中onCreate()
,如下所示:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(false);
}
However, the menu that is being shown that you would like to hide (REFRESH), belongs to MainActivity. That is why it is always shown. Since you want to control the menu at the Fragment
level (and not show an Activity
options menu), my suggestion is to delete the menu code from the Activity
and implement it in your Fragment
.
但是,正在显示的要隐藏(刷新)的菜单属于 MainActivity。这就是它始终显示的原因。由于您想在Fragment
级别控制菜单(而不是显示Activity
选项菜单),我的建议是从 中删除菜单代码Activity
并在您的Fragment
.
Activity
s and Fragment
s can each have their own separate menus. See this link.
Activity
s 和Fragment
s 都可以有自己独立的菜单。请参阅此链接。
回答by pratham kesarkar
In the fragment where you want to hide the Item
在要隐藏项目的片段中
@Override
public void onPrepareOptionsMenu(Menu menu) {
MenuItem item=menu.findItem(R.id.action_search);
if(item!=null)
item.setVisible(false);
}
and in onCreate() of your fragment
并在您的片段的 onCreate() 中
setHasOptionsMenu(true);
回答by Sandy Akbar
please try this
请试试这个
@Override
public void onPrepareOptionsMenu(Menu menu) {
menu.clear();
}
and put this on your fragmen's onCreate()
并将其放在您的片段的 onCreate() 上
setHasOptionsMenu(true);
回答by Divyesh V.Murani
In Fragment Class
在片段类中
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
menu.clear();
}
回答by Android27
I used the code below for hiding menu items in a fragment where I don't want to use it. Note: Please read comment
我使用下面的代码在我不想使用的片段中隐藏菜单项。注意:请阅读评论
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
Fragment whichFragment=getVisibleFragment();//getVisible method return current visible fragment
String shareVisible=whichFragment.getClass().toString();
if(shareVisible.equals(AccFragment.class.toString())
||shareVisible.equals(SocFragment.class.toString())
||shareVisible.equals(DevFragment.class.toString())
){
MenuItem item=menu.findItem(R.id.action_share);
item.setVisible(false);
}
return super.onCreateOptionsMenu(menu);
}
回答by Wahib Ul Haq
There are many different versions of similar solutions but unfortunately, none of them worked for me. I am sharing what eventually was useful for me to hide the whole overflow menu with multiple menu items. Thought maybe it's useful for anyone.
有许多不同版本的类似解决方案,但不幸的是,没有一个对我有用。我正在分享最终对我有用的东西,以隐藏带有多个菜单项的整个溢出菜单。想也许它对任何人都有用。
I grouped my menus with an id
and then referred that id
我用 将我的菜单分组id
,然后引用id
@Override
public void onPrepareOptionsMenu(Menu menu) {
menu.setGroupVisible(R.id.menu_overflow, false);
super.onPrepareOptionsMenu(menu);
}
If you want to hide any individual menu item then you can use
如果要隐藏任何单个菜单项,则可以使用
menu.getItem(R.id.action_licenses).setVisible(false);
Important thing is that you should have setOptionsMenu(true)
in onViewCreated()
重要的是你应该setOptionsMenu(true)
在onViewCreated()
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
setHasOptionsMenu(true);
回答by Aziz
Call setHasOptionMenu(true)
in onCreateView()
通话setHasOptionMenu(true)
中onCreateView()
and Do not callsuper.onCreateOptionsMenu()
in fragment's onCreateOptionMenu()
instead call menu.clear()
because this will override the existing menu with the activity's menu
并且不要super.onCreateOptionsMenu()
在片段中调用onCreateOptionMenu()
而是调用,menu.clear()
因为这将使用活动的菜单覆盖现有菜单
This worked in my case.
这在我的情况下有效。
回答by Henk-Martijn
Or solve it in the same Fragment which created the menu, if you host the Actionbar on Activity level. This way you don't have to add it on every other Fragment where you don't want to show it:
或者在创建菜单的同一个 Fragment 中解决它,如果您在 Activity 级别托管 Actionbar。这样您就不必将它添加到您不想显示的所有其他 Fragment 上:
public override void OnDestroy()
{
base.OnDestroy();
HasOptionsMenu = false;
}
回答by Null Pointer Exception
Add these functions to your Fragment
将这些函数添加到您的 Fragment
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public void onPrepareOptionsMenu(Menu menu) {
MenuItem item=menu.findItem(R.id.delete);
item.setVisible(false);
}
回答by androidGeek
Firstly in your Activity
that has the toolbar, create a method that sets up the overflow menu
for you:
首先在你Activity
有工具栏的地方,创建一个overflow menu
为你设置的方法:
public void setUpOptionMenu(Toolbar toolbar){
this.setSupportActionBar(toolbar);
}
In your fragments onCreateView()
method, get the reference of your current activity and call your activities setUpOptionMenu()
method:
在您的片段onCreateView()
方法中,获取您当前活动的引用并调用您的活动setUpOptionMenu()
方法:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
...
...
public void ((YourActivityName)this.getActivity()).setUpOptionMenu(null);
...
...
}
Cheers!!!
干杯!!!