操作按钮会出现在所有碎片

操作按钮会出现在所有碎片

本文介绍了操作按钮会出现在所有碎片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我已经包含在我的片段菜单。
但在我viewpager前往其他片段时(未覆盖菜单)的操作按钮保持不变(所有的动作)设置setHasOptionsMenu即使(假)。

任何人都知道如何在其他片段禁用操作按钮,或者你需要在每个片段膨胀空菜单?

在此先感谢

这是我的code(在片段):

  @覆盖    公共无效onCreateOptionsMenu(菜单菜单,MenuInflater气筒){
    super.onCreateOptionsMenu(菜单,充气器);
    menu.clear();
    inflater.inflate(R.menu.main,菜单);    }@覆盖
公共无效的onCreate(捆绑savedInstanceState){
    // TODO自动生成方法存根
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(真);
}


解决方案

您需要调用 setHasOptionsMenu 片段 onCreateView ,告诉活动,该片段有选项菜单。使 onCreateOptionsMenu 将被调用。

  @覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
                         捆绑savedInstanceState){
    //充气的布局该片段
    查看排= inflater.inflate(R.layout.fragment_main,集装箱,FALSE);    setHasOptionsMenu(真);
    返回行;
}

,并在 onCreateOptionsMenu ,只清除菜单项

  @覆盖
公共无效onCreateOptionsMenu(菜单菜单,MenuInflater气筒){
    menu.clear();
    super.onCreateOptionsMenu(菜单,充气器);
}

I have included a menu in my fragment.But when going to other fragments(not overriding menu) in my viewpager the actions buttons remains the same (with all the actions) even when setting setHasOptionsMenu(false).

Anyone knows how to disable action buttons in other fragments or you need to inflate an empty menu in each fragment ?

Thanks in advance

This is my code (in fragment) :

@Override

    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    menu.clear();
    inflater.inflate(R.menu.main, menu);

    }

@Override
public void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);


}
解决方案

You need to call setHasOptionsMenu from the Fragment onCreateView , to tell the Activity that the Fragment has Option Menu. So that the onCreateOptionsMenu will be called.

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View row =  inflater.inflate(R.layout.fragment_main, container, false);

    setHasOptionsMenu(true);
    return row;
}

And, In onCreateOptionsMenu , just clear the menu items

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    menu.clear();
    super.onCreateOptionsMenu(menu, inflater);
}

这篇关于操作按钮会出现在所有碎片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 19:48