片段内片段中的上下文菜单(ActionMode)

时间:2012-05-23 10:30:51

标签: android menu android-fragments actionbarsherlock

我在活动中的片段A中获得了片段B.按预期工作。 单击片段B中的项目时,我想显示contextual menu栏。

我正在使用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()]");
}

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

  

contextualMode已初始化[onItemClick()] entry.selected set   到'true'[onItemClick()]

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

1 个答案:

答案 0 :(得分:1)

  

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

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