ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

列表视图项的Android工具栏上下文菜单

2019-08-24 16:27:36  阅读:196  来源: 互联网

标签:android android-toolbar contextmenu android-listview menu


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

解决方法:

检查this official android guide.

编辑:

使用上下文操作模式

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

>用户在视图上执行长按.
>用户在中选择一个复选框或类似的UI组件
视图.

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

>对于单个任意视图的上下文操作.
>对于ListView中的项目组的批处理上下文操作
GridView(允许用户选择多个项目并执行
对他们采取行动).

为各个视图启用上下文操作模式

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

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

>如果要显示栏(例如用户长按视图时),请调用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接口并设置
它用于具有setMultiChoiceModeListener()的视图组.在里面
监听器的回调方法,可以为其指定动作
上下文操作栏,响应操作项上的单击事件,以及
处理从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.

标签:android,android-toolbar,contextmenu,android-listview,menu
来源: https://codeday.me/bug/20190824/1709867.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有