操作按钮添加到操作栏一个片段 [英] Add Action Button to Action Bar For One Fragment

查看:186
本文介绍了操作按钮添加到操作栏一个片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序有三个选项卡,我用呼机的标签之间切换。每个标签都有自己的片段。我使用OptionsMenu方法添加的设置和帮助操作按钮的操作栏的主要活动。我现在想添加一个新的操作按钮,操作栏,只是第一个选项卡,第一个片段,我不希望它的时候都显示在其标签上的其他片段出现。

My app has three tabs and I am using a pager to switch between the tabs. Each tab has its own fragment. I added the Settings and Help action buttons to the action bar in the main Activity using the OptionsMenu methods. I now want to add a new action button to the action bar, but just for the first tab and first fragment, and I don't want it to appear on the other fragments when they are displayed in their tabs.

我有这种布局是主活动创建的主菜单 -
    

I have this layout for the main menu that is created in the main activity -

    <item android:id="@+id/action_settings"
        android:title="@string/action_settings"
        android:orderInCategory="100" app:showAsAction="never" />

    <item android:id="@+id/help"
        android:title="@string/help"
        android:orderInCategory="100" app:showAsAction="never" />
</menu>

本菜单显示正确,按预期工作。

This menu displays correctly and works as expected.

我还有一个菜单,menu_ prelaunch_fragment的第一个片段 -

I have another menu, menu_prelaunch_fragment for the first fragment -

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item android:id="@+id/clear_form"
        android:title="Clear"
        android:icon="@drawable/ic_action_action_delete"
        app:showAsAction="always" />
</menu>

和我使用它在第一个片段以下code添加到操作栏 -

and I add it to the action bar using the following code in the first fragment -

@Override
public void onCreate(Bundle savedInstance) {
    Log.d(TAG, "onCreate");
    super.onCreate(savedInstance);
    setHasOptionsMenu(true);
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflator) {
    Log.d(TAG, "onCreateOptionsMenu");
    inflator.inflate(R.menu.menu_prelaunch_fragment, menu);
    super.onCreateOptionsMenu(menu, inflator);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    Log.d(TAG, "onOptionsItemSelected");
    if (item.getItemId() == R.id.clear_form)
        clearFragmentData();
    return super.onOptionsItemSelected(item);
}

问题是,这种添加的按钮并没有消失,当我去到另一个片段上的其他网页/标签。其他碎片没有任何选项菜单code在其中。

The problem is that this added button does not go away when I go to the other fragments on the other pages/tabs. The other fragments do not have any options menu code in them.

然后我加入这个code到其他片段删除一个按钮。

I then added this code to the other fragments to remove the one button.

@Override
public void onCreate(Bundle savedInstanceState) {
    Log.d(TAG, "onCreate");
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
}

@Override
public void onPrepareOptionsMenu (Menu menu) {
    menu.findItem(R.id.clear_form).setVisible(false);
    super.onPrepareOptionsMenu(menu);
}

但现在的清除按钮不会出现在动作条,无论选择什么样的选项卡。

But now the clear button does not appear in the action bar regardless of what tabs are selected.

如何在我的三个选项卡度(即三个片段)的应用程序添加一个动作按钮的操作栏只是一个选项卡(即片段)?它应该只出现在选择该选项卡(片段)时(显示)。

How do I add an action button to the action bar just for one tab (ie fragment) in my three tab (ie three fragment) app? It should only appear when that tab (fragment) is selected (displayed).

谢谢!

推荐答案

这是我如何解决在视图中寻呼机有只有一个页面的操作栏图标的问题。

This is how I solved the problem of having an icon in the action bar for only one page in a view pager.


  1. 创建完整菜单,包括只应显示特定页面的主要活动通常的方式的项目。保存到在应用程序对象中的每个页面更改菜单项的参考。

  1. Create the full menu, including the item that only should be displayed for a particular page, in the main activity the usual way. Save a reference to the menuItem that changes for each page in the Application object.

实施onPageChangelistener如上,并根据需要对每个页面设定的菜单项的可见性。你从Application对象访问菜单项。这使菜单项,只要你想他们在一个特定的手机定位,但会失败时的电话改变了方向。保存在应用程序对象中的当前页面位置。一定要在初始化Application对象的页面位置到第一页(即0)。

Implement the onPageChangelistener as above, and set the visibility of the menuItem as needed for each page. You get access to the menuItem from the Application object. This keeps the menu items as you want them in a particular phone orientation, but will fail when the orientation of the phone changes. Save the current page position in the Application object. Be sure to initialize the page position in the Application object to the first page (ie 0).

在onCreateOptionsMenu的主要活动,使用当前页位置(在Application对象中找到),以确定该菜单项应该是可见或不可见。这将需要屏幕旋转的照顾。

In the onCreateOptionsMenu in the main activity, use the current page position (found in the Application object) to determine if the menuItem should be visible or not. This will take care of screen rotations.

这篇关于操作按钮添加到操作栏一个片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆