Android工具栏上下文菜单用于ListView项。

3

我正在寻求如何在工具栏中为ListView项目实现类似WhatsApp的上下文菜单的帮助。到目前为止,我找到的所有教程都是关于弹出对话框的,这不是我想要的。是否有人可以帮助我或提供链接和教程?谢谢 :)

1个回答

6

请查看这个官方的Android指南

编辑:

使用上下文操作模式

对于提供上下文操作的视图,通常应在以下两种事件之一(或两者都)上调用上下文操作模式:

  • 用户长按视图。
  • 用户选择视图中的复选框或类似的UI组件。

如何调用上下文操作模式以及为每个操作定义行为取决于您的设计。基本上有两种设计:

  • 针对单个任意视图的上下文操作。
  • 针对ListView或GridView中的项目组的批量上下文操作(允许用户选择多个项目并对它们执行操作)。

启用单个视图的上下文操作模式

  1. Implement the ActionMode.Callback interface. In its callback methods, you can specify the actions for the contextual action bar, respond to click events on action items, and handle other lifecycle events for the action mode.

    private ActionMode.Callback mActionModeCallback = new             ActionMode.Callback() {
    
    // Called when the action mode is created; startActionMode() was called
    @Override
    public boolean onCreateActionMode(ActionMode mode, Menu menu) {
        // Inflate a menu resource providing context menu items
        MenuInflater inflater = mode.getMenuInflater();
        inflater.inflate(R.menu.context_menu, menu);
        return true;
    }
    
    // Called each time the action mode is shown. Always called after onCreateActionMode, but
    // may be called multiple times if the mode is invalidated.
    @Override
    public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
        return false; // Return false if nothing is done
    }
    
    // Called when the user selects a contextual menu item
    @Override
    public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_share:
                shareCurrentItem();
                mode.finish(); // Action picked, so close the CAB
                return true;
            default:
                return false;
        }
    }
    
    // Called when the user exits the action mode
    @Override
    public void onDestroyActionMode(ActionMode mode) {
        mActionMode = null;
    }
    };
    
  2. Call startActionMode() when you want to show the bar (such as when the user long-clicks the view).

    someView.setOnLongClickListener(new View.OnLongClickListener() {
    // Called when the user long-clicks on someView
    public boolean onLongClick(View view) {
        if (mActionMode != null) {
            return false;
        }
    
        // Start the CAB using the ActionMode.Callback defined above
        mActionMode = getActivity().startActionMode(mActionModeCallback);
        view.setSelected(true);
        return true;
    }
    });
    

在ListView或GridView中启用批量上下文操作

如果您在ListView或GridView(或AbsListView的其他扩展)中有一组项目,并希望允许用户执行批处理操作,则应该:

  • Implement the AbsListView.MultiChoiceModeListener interface and set it for the view group with setMultiChoiceModeListener(). In the listener's callback methods, you can specify the actions for the contextual action bar, respond to click events on action items, and handle other callbacks inherited from the ActionMode.Callback interface.
  • Call setChoiceMode() with the CHOICE_MODE_MULTIPLE_MODAL argument.

    ListView listView = getListView();
    listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
    listView.setMultiChoiceModeListener(new MultiChoiceModeListener() {
    
    @Override
    public void onItemCheckedStateChanged(ActionMode mode, int position,
                                          long id, boolean checked) {
        // Here you can do something when items are selected/de-selected,
        // such as update the title in the CAB
    }
    
    @Override
    public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
        // Respond to clicks on the actions in the CAB
        switch (item.getItemId()) {
            case R.id.menu_delete:
                deleteSelectedItems();
                mode.finish(); // Action picked, so close the CAB
                return true;
            default:
                return false;
        }
    }
    
    @Override
    public boolean onCreateActionMode(ActionMode mode, Menu menu) {
        // Inflate the menu for the CAB
        MenuInflater inflater = mode.getMenuInflater();
        inflater.inflate(R.menu.context, menu);
        return true;
    }
    
    @Override
    public void onDestroyActionMode(ActionMode mode) {
        // Here you can make any necessary updates to the activity when
        // the CAB is removed. By default, selected items are deselected/unchecked.
    }
    
    @Override
    public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
        // Here you can perform updates to the CAB due to
        // an invalidate() request
        return false;
    }
    });
    

想了解更多菜单功能,请点击此链接


1
尽管此链接可能回答了问题,但最好在此处包括答案的必要部分并提供参考链接。如果链接页面发生更改,则仅有链接的答案可能会失效。 - Taifun
@Taifun 感谢您帮助我改进我的答案。希望我做得对。 - Anton Kovalyov

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接