【问题标题】:ActionBar Badge returns optionitemselected click to Activity instead of my FragmentActionBar Badge 返回 optionitemselected click 到 Activity 而不是我的 Fragment
【发布时间】:2015-02-13 18:29:43
【问题描述】:

我的片段中有 onOptionsItemSelected(MenuItem item)。现在我不得不使用 Android-ActionItemBadge 库(https://github.com/mikepenz/Android-ActionItemBadge)来添加 ActionBar 通知计数。 所以我在我的 Fragment 中添加了这段代码。

public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
            //Inflating the Menu
                inflater.inflate(R.menu.refresh_menu, menu);

        //inflating Notification Icon
        if (badgeCount > 0) {
            ActionItemBadge.update(getActivity(), menu.findItem(R.id.badge),
                    FontAwesome.Icon.faw_android, ActionItemBadge.BadgeStyle.DARKGREY, badgeCount);
        } else {
            ActionItemBadge.hide(menu.findItem(R.id.badge));
        }
}

但是这个 Optionitemselected 将值返回到我的 Activity 中,而不是返回到我的 Fragment 中。任何的想法?我想处理在我的 Fragment 中选择的这个 Optionitem。

【问题讨论】:

    标签: android android-activity android-fragments android-fragmentactivity android-actionbar-compat


    【解决方案1】:

    在你的片段中你需要调用:

    setHasOptionsMenu(true);
    

    编辑:

    由于这个 custom ActionBar 项目没有提供对您的片段的调用,您可以简单地手动进行:

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_search: // Your item id
                Fragment f = getFragmentManager().findFragmentById(R.id.fragment_container);
                f.onOptionsItemSelected(item);
                break;
            default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }
    

    【讨论】:

    • 我已启用,其他选项工作正常。只有这个 ActionItemBadge 正在处理 MainActivity 中的点击事件。
    • @Vrangle 我明白了。你可以手动完成,就像我更新的答案一样。
    • 工作得很好干杯。一点更清楚,以便其他人可以理解。我已经在我的主 Activity 中实现了这个代码,并将调用转移到我的 Fragments 并且我的 Fragments 有它自己的 onOptionsItemSelected(MenuItem item) 方法来处理它。干得好用户3249477
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多