1

我在 Activity 的 Fragment A 中得到了 Fragment B。按预期工作。单击片段 B 中的项目时,我想显示一个上下文菜单栏。

我正在使用 ActionbarSherlock。我在片段 B 中做了什么:

private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {

    @Override
    public boolean onCreateActionMode(ActionMode mode, Menu menu) {
        MenuInflater inflater = mode.getMenuInflater();
        inflater.inflate(R.menu.entry_list_context_menu, menu);
        return true;
    }

    @Override
    public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
        // make sure no item is selected when bar is shown
        adapter.clearSelection();
        adapter.notifyDataSetChanged();
        return false;
    }

    @Override
    public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
        Log.d("EntryList", "Item '" + item.getTitle()
                + "' clicked [onActionItemClicked()]");
        return true;
    }

    @Override
    public void onDestroyActionMode(ActionMode mode) {
        adapter.clearSelection();
        adapter.notifyDataSetChanged();
        contextualMode = null;
    }
};
private ActionMode contextualMode;

@Override
public void onItemClick(AdapterView<?> parentView, View itemView,
        int index, long id) {
    DocumentEntity entry = (DocumentEntity) itemView.getTag();
    // something went wrong
    if (entry == null) {
        Log.e("EntryList", "Tag-Less item clicked [onItemClick()]");
        return;
    }

    if (contextualMode != null) {
        Log.d("EntryList",
                "contextualMode is not yet initialized [onItemClick()]");
        contextualMode = getSherlockActivity().startActionMode(
                mActionModeCallback);
    } else {
        Log.d("EntryList",
                "contextualMode already initialized [onItemClick()]");
    }

    entry.setSelected(!entry.isSelected());
    Log.d("EntryList", "entry.selected set to " + entry.isSelected()
            + " [onItemClick()]");
}

选择效果很好,但没有显示上下文操作栏。调试结果是:

contextualMode 已初始化 [onItemClick()] entry.selected 设置为 'true' [onItemClick()]

没有设置 contextualMode 的其他位置...

4

1 回答 1

1

我在片段 A 中得到了片段 B...

Android 不支持在另一个 Fragment 中嵌入一个 Fragment。对不起。这使我相信您的问题超出了ActionBar没有显示您的上下文的事实。我建议你澄清你原来的帖子。

于 2012-05-26T04:53:23.820 回答