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

标签 android android-listview menu contextmenu android-toolbar

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

最佳答案

检查 this official android guide.

编辑:

使用上下文操作模式

对于提供上下文操作的 View ,您通常应该在两个事件之一(或两个事件)上调用上下文操作模式:

  • 用户长按 View 。
  • 用户在 查看。

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

  • 针对个人、任意 View 的上下文操作。
  • 用于对 ListView 中的项目组进行批量上下文操作或 GridView(允许用户选择多个项目并执行 对他们采取行动)。

为单个 View 启用上下文操作模式

  1. 实现 ActionMode.Callback 接口(interface)。在其回调方法中,您可以指定上下文操作栏的操作,响应操作项上的点击事件,以及处理操作模式的其他生命周期事件。

    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. 当您想要显示栏时(例如当用户长按 View 时)调用 startActionMode()。

    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 的另一个扩展)中有一个项目集合,并且希望允许用户执行批处理操作,您应该:

  • 实现AbsListView.MultiChoiceModeListener接口(interface)并设置 它用于带有 setMultiChoiceModeListener() 的 View 组。在里面 监听器的回调方法,你可以指定的 Action 上下文操作栏,响应操作项上的点击事件,以及 处理从 ActionMode.Callback 继承的其他回调 界面。
  • 使用 CHOICE_MODE_MULTIPLE_MODAL 参数调用 setChoiceMode()。

    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;
    }
    });
    

更多菜单功能 check this link.

关于用于 ListView 项的 Android 工具栏上下文菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30686898/

相关文章:

Android和使用sqlite的两点之间的距离

android - 如何显示选择输入法对话框

php - 下拉菜单在 drupal 中不起作用

html - 菜单项隐藏在数据列表后面

android - 加载 XML 文件抛出 RuntimeException

android将工具栏标题颜色更改为白色

android - 登录然后 ListView 的基本方法

java - 提高当前使用数据库计数聚合的 Android ListView 的性能

java - 安卓 ListView : How to add a row with a buttonclick

css - 如何制作元素之间可变空间的 ul li css 菜单