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

列表视图项的Android工具栏上下文菜单,第1张

概述我正在寻找有关如何在ListView项目的工具栏中实现上下文菜单的帮助,如WhatsApp所做的那样.到目前为止,我发现的唯一教程是关于d出的对话框,这不是我想要的.有人可以帮助我或提供教程链接吗?谢谢:)解决方法:检查thisofficialandroidguide.编辑:使用上下文 *** 作模式对于提供上下

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

解决方法:

检查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@OverrIDepublic 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.@OverrIDepublic boolean onPrepareActionMode(ActionMode mode, Menu menu) {    return false; // Return false if nothing is done}// Called when the user selects a contextual menu item@OverrIDepublic 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@OverrIDepublic voID onDestroyActionMode(ActionMode mode) {    mActionMode = null;}};

>如果要显示栏(例如用户长按视图时),请调用startActionMode().

someVIEw.setonLongClickListener(new VIEw.OnLongClickListener() {// Called when the user long-clicks on someVIEwpublic 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() {@OverrIDepublic 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}@OverrIDepublic 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;    }}@OverrIDepublic boolean onCreateActionMode(ActionMode mode, Menu menu) {    // Inflate the menu for the CAB    MenuInflater inflater = mode.getMenuInflater();    inflater.inflate(R.menu.context, menu);    return true;}@OverrIDepublic 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.}@OverrIDepublic 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工具栏上下文菜单所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1110877.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存