Android Contextual Menus之二:contextual action mode

  接上文:Android Contextual Menus之一:floating context menu

  ContextMenu的两种形式,上文讨论了第一种形式,兼容性较好。

  本文讨论第二种形式,Android 3.0,即API Level 11之后可用。

Contextual action mode

  Contextual action mode是 ActionMode 的系统实现,关注于执行上下文相关动作的用户交互。

  当用户通过选择一个项目使能这个模式,一个contextual action bar就会出现在屏幕上方,显示用户对当前选中的项目可以执行的动作。

  当这个模式使能时,用户可以:选择多个项目(如果你允许的话)、取消项目选择、在activity中继续浏览(只要你允许)。

  当用户取消对所有项目的选择、按下Back键、或者点击bar左边的完成按钮之后,action mode就被禁用,contextual action bar消失。

  注意:contextual action bar没有必须 action bar关联,它们是独立的。

CAB的使用情形

  对于提供上下文动作的View,通常在这两种情况下(情况之一或both)调用contextual action mode

  1.用户在View上长按;

  2.用户选择了View中的CheckBox或者类似控件。

  你的应用如何invoke这个contextual action mode,以及如何定义每个action取决于你自己的设计。

  两种基本的设计:

  1.对个体任意views的上下文相关操作;

  For contextual actions on individual, arbitrary views.

  2.对一组数据的批处理,比如ListView或GridView中的项目,允许用户选择多个项目然后对它们整体执行一个动作。

  For batch contextual actions on groups of items in a ListView or GridView (allowing the user to select multiple items and perform an action on them all).

  下面分别讲讲这两种情景下的实现。

Enabling the contextual action mode for individual views

  如果你想在用户选择指定View的时候invoke contextual action mode(CAB),你应该:

  1.实现ActionMode.Callback接口。

  在这个接口的回调方法中,你可以指定contextual action bar的动作,响应action items的点击事件,还有处理action mode的生命周期事件。

  2.当你想要show这个bar的时候(比如用户长按view的时候),调用 startActionMode()方法。

  例子代码:

package com.example.mengdd.hellocontextmenu;

import android.app.Activity;
import android.os.Bundle;
import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.widget.TextView;
import android.widget.Toast; public class ContextualActionModeActivity extends Activity { private TextView mTextView = null;
private ActionMode mActionMode = null; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contextual_action_mode); mTextView = (TextView) findViewById(R.id.textView2);
mTextView.setOnLongClickListener(new OnLongClickListener() { @Override
public boolean onLongClick(View view) {
if (mActionMode != null) {
return false;
} // Start the CAB using the ActionMode.Callback defined above
mActionMode = startActionMode(mActionModeCallback);
view.setSelected(true);
return true;
}
});
} 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_menu1, 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.edit: showEditor();
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;
}
}; private void showEditor() {
Toast.makeText(ContextualActionModeActivity.this, "edit",
Toast.LENGTH_LONG).show();
}
}

CAB Demo1

Enabling batch contextual actions in a ListView or GridView

  对于ListView和GridView这样的集合类,想让用户进行批处理操作,应该如下:

  1.实现 AbsListView.MultiChoiceModeListener接口,通过setMultiChoiceModeListener()方法把它set进集合类控件。

  在这个listener的回调方法中,你可以指定contextual action bar的动作,响应action item的点击事件,处理其他继承自ActionMode.Callback的回调。

  2.调用 setChoiceMode()方法,使用参数 CHOICE_MODE_MULTIPLE_MODAL 。

  例子代码:

package com.example.mengdd.hellocontextmenu;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MenuInflater;
import android.widget.AbsListView.MultiChoiceModeListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast; public class ListCABActivity extends ListActivity {
private ListView mListView = null;
private String[] mStrings = Cheeses.sCheeseStrings; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Use an existing ListAdapter that will map an array
// of strings to TextViews
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, mStrings)); mListView = getListView();
mListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL); mListView.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.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_menu2, 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;
}
}); } private void deleteSelectedItems() {
Toast.makeText(ListCABActivity.this, "delete!", Toast.LENGTH_LONG)
.show();
}
}

CAB Demo2

参考资料

  API Guides: Menus->Using the contextual action mode

  http://developer.android.com/guide/topics/ui/menus.html#CAB

Android Contextual Menus之二:contextual action mode的更多相关文章

  1. Android Contextual Menus之一:floating context menu

    Android Contextual Menus之一:floating context menu 上下文菜单 上下文相关的菜单(contextual menu)用来提供影响UI中特定item或者con ...

  2. Android官方导航栏ActionBar(二)—— Action View、Action Provider、Navigation Tabs的详细用法

    在上一篇文章(Android之官方导航栏ActionBar)中,我们介绍了ActionBar各组成部分的基本应用.ActionBar除了提供Action Buttons外,还提供了多种导航方式如 Ac ...

  3. Android系统UI交互控件Action Bar初探

    过年期间,Google正式宣布取消Android系统中MENU键的使用,也就是基于Android 4.0系统的手机都应没有MENU这一固定按键.这无疑是个变革性的改动,在我眼中,这似乎把Android ...

  4. Android开发——通过扫描二维码,打开或者下载Android应用

    Android开发——通过扫描二维码,打开或者下载Android应用   在实现这个功能的时候,被不同的浏览器折磨的胃疼,最后实现了勉强能用,也查考了一下其他人的博客 android实现通过浏览器点击 ...

  5. Android学习路线(二十四)ActionBar Fragment运用最佳实践

    转载请注明出处:http://blog.csdn.net/sweetvvck/article/details/38645297 通过前面的几篇博客.大家看到了Google是怎样解释action bar ...

  6. Android百度地图(二)结合方向传感器我们自己定位哪里走

    Android百度地图(二)结合方向传感器我们自己定位哪里走 本文代码在http://blog.csdn.net/xyzz609/article/details/51943556的基础上进一步改动.有 ...

  7. 二维码合成,将苹果和安卓(ios和android)合成一个二维码,让用户扫描一个二维码就可以分别下载苹果和安卓的应用

    因为公司推广的原因,没有合适的将苹果和安卓(ios和android)合成一个二维码的工具. 因为这个不难,主要是根据浏览器的UA进行判断,所以就自己开发了一个网站 网站名称叫:好推二维码  https ...

  8. Android APP压力测试(二)之Monkey信息自动收集脚本

      Android APP压力测试(二) 之Monkey信息自动收集脚本 前言: 上一篇Monkey介绍基本搬抄官方介绍,主要是为了自己查阅方便.本文重点介绍我在进行Monkey时如何自动收集相关信息 ...

  9. Android项目实战(二十八):Zxing二维码实现及优化

    前言: 多年之前接触过zxing实现二维码,没想到今日项目中再此使用竟然使用的还是zxing,百度之,竟是如此牛的玩意. 当然,项目中我们也许只会用到二维码的扫描和生成两个功能,所以不必下载完整的ja ...

随机推荐

  1. javascript学习总结(二):DOM常用方法。

    1 获取元素节点 a document.getElementById(id),它返回一个对象.是Document对象特有的函数,它还有这些方法: b element.getElementsByTagN ...

  2. 如何给wordpress首页自动显示文章内容的第一个图片

    敏捷个人手机应用中使用到的数据来源于wordpress中,因为自己写的页面,所以可以自己写代码获取文章内容的第一个图片作为文章缩略图来显示,这样用户看到首页时图文并茂,感觉会好一些. 现在后台简单的使 ...

  3. idea快捷键总结

    使用好快捷键会快很多,这里我慢慢添加我用习惯的快捷键.参考 1.alt+enter 这个几乎万能,有错误提示的时候将光标移动到错误处,然后alt+enter,会给出建议方案:写完一个表达式后,alt+ ...

  4. SQL Server代理(3/12):代理警报和操作员

    SQL Server代理是所有实时数据库的核心.代理有很多不明显的用法,因此系统的知识,对于开发人员还是DBA都是有用的.这系列文章会通俗介绍它的很多用法. 如我们在这个系列的文章里所见,SQL Se ...

  5. CentOS matplotlib 安装

    sudo yum install freetype-devel sudo yum install libpng-devel sudo pip install matplotlib

  6. C语言学习007:重定向标准输入和输出

    先来完成一个将输入数据转换成json格式输出的小任务 #include <stdio.h> int main(){ float latitude; float longtitude; ]; ...

  7. char类型的说明

    CREATE TABLE [dbo].[CharTest]( ) NULL, ) NULL, ) NULL, ) NULL ) insert into dbo.CharTest ( Char, Var ...

  8. MySql基础整理

    http://hovertree.com/menu/mysql/ use abccs;select * from mytable2 limit 3,4;call sp_name1(1,@nn);sel ...

  9. iOS阶段学习第20天笔记(MRC内存管理)

    iOS学习(OC语言)知识点整理 一.OC中的内存管理 1)概念:内存管理的对象为所有继承了NSObject的对象,对基本数据(如:int .float.double...)无效      OC中采用 ...

  10. 修复 XE7 update1 发布 iOS 8.x 实机问题

      1. 开启工程目录下面的 Entitlement.TemplateiOS.xml 档案. 2. 加入二行: <key>application-identifier</key> ...