我怎样才能设置一个动作条项目的不同片段的知名度知名度、片段、不同、动作

2023-09-06 10:12:30 作者:拦精灵

我正在寻找一种方法来设置的能见度菜单项充气我MainActivity取决于在其中片段我在

I'm looking for a way to set the visibility of a MenuItem inflated in my MainActivity depending on which Fragment I am on.

有关信息:我使用 actionBarSherlock zxing ,有的 Google服务

For information: I'm using actionBarSherlock, zxing, and some google services.

应用程序内建一个抽屉式导航(带ABS),我也操作 FragmentStack 以这种方式,我每次我切换到另一个片段时,我preSS触摸背我回来在我的主要片段

The application was built with a Navigation drawer(With abs), also I manipulate the FragmentStack in such way I everytime I switch to another Fragment when I press the touch back I come back in my Main Fragment.

下面我的菜单:

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
   <item android:id="@+id/button_generator" android:title="GENERER" android:icon="@drawable/ic_drawer"></item>
</menu>

下面是我吹菜单:

 @Override
public boolean onCreateOptionsMenu(Menu menu) {
    Log.d(TAG, "================= onCreateOptionsMenu ================= fragSt: " + fragmentStatus);
    this.getSherlock().getMenuInflater().inflate(R.menu.main, menu);

    mGenQrFromContacts = menu.findItem(R.id.button_generator);


    return true;
}

我已经试过了解决方案意here,但在我的情况下工作。

I've tried the solution purposed here, but ain't work in my case.

推荐答案

您应该尝试在你的片段

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // ...  
    // call the method setHasOptionsMenu, to have access to the menu from your fragment
    setHasOptionsMenu(true);

    //...
}

// the create options menu with a MenuInflater to have the menu from your fragment
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    menu.findItem(R.id.button_generator).setVisible(true);
    super.onCreateOptionsMenu(menu, inflater);
}  

而这一点,在你的活动

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getSupportMenuInflater().inflate(R.menu.my_layout, menu);
    menu.findItem(R.id.button_generator).setVisible(false);
    return true;
}

希望这有助于。

Hope this helps.