当前位置: 首页 > news >正文

Android Contextual Menus之二:contextual action mode

 

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

 

相关文章:

  • 基本类型和引用类型的值 动态的属性
  • JVM启动参数小结
  • java B2B2C源码电子商务平台 -----客户端负载均衡策略
  • 八年技术加持,性能提升10倍,阿里云HBase 2.0首发商用
  • 构建基于WCF Restful Service的服务
  • Python基础10_动态传参,名称空间和作用域,函数嵌套,关键字global和nonlocal
  • Log4j.properties配置详解
  • Linux环境搭建 | 使用WinSCP远程连接虚拟机
  • Spring boot JPA 用自定义主键策略 生成自定义主键ID
  • java 环境 eclipse 配置
  • 福大软工 · 第十次作业 - 项目测评(团队) [已完成]
  • Html页面插入flash代码
  • 关于自定义 Alert
  • C#练习4
  • python 基础语法 - 函数(一)
  • 【划重点】MySQL技术内幕:InnoDB存储引擎
  • 11111111
  • android百种动画侧滑库、步骤视图、TextView效果、社交、搜房、K线图等源码
  • Nginx 通过 Lua + Redis 实现动态封禁 IP
  • node-glob通配符
  • spring-boot List转Page
  • vue从入门到进阶:计算属性computed与侦听器watch(三)
  • 从@property说起(二)当我们写下@property (nonatomic, weak) id obj时,我们究竟写了什么...
  • 从零到一:用Phaser.js写意地开发小游戏(Chapter 3 - 加载游戏资源)
  • 基于阿里云移动推送的移动应用推送模式最佳实践
  • 简单基于spring的redis配置(单机和集群模式)
  • 理解 C# 泛型接口中的协变与逆变(抗变)
  • 排序(1):冒泡排序
  • 如何使用Mybatis第三方插件--PageHelper实现分页操作
  • 如何优雅的使用vue+Dcloud(Hbuild)开发混合app
  • 实现菜单下拉伸展折叠效果demo
  • - 转 Ext2.0 form使用实例
  • ​​​​​​​​​​​​​​Γ函数
  • #Linux杂记--将Python3的源码编译为.so文件方法与Linux环境下的交叉编译方法
  • ( )的作用是将计算机中的信息传送给用户,计算机应用基础 吉大15春学期《计算机应用基础》在线作业二及答案...
  • ( 用例图)定义了系统的功能需求,它是从系统的外部看系统功能,并不描述系统内部对功能的具体实现
  • (11)MSP430F5529 定时器B
  • (3)llvm ir转换过程
  • (3)nginx 配置(nginx.conf)
  • (9)YOLO-Pose:使用对象关键点相似性损失增强多人姿态估计的增强版YOLO
  • (Java数据结构)ArrayList
  • (PyTorch)TCN和RNN/LSTM/GRU结合实现时间序列预测
  • (附源码)springboot工单管理系统 毕业设计 964158
  • (附源码)基于SpringBoot和Vue的厨到家服务平台的设计与实现 毕业设计 063133
  • (附源码)计算机毕业设计ssm本地美食推荐平台
  • (黑马出品_高级篇_01)SpringCloud+RabbitMQ+Docker+Redis+搜索+分布式
  • (南京观海微电子)——I3C协议介绍
  • (算法)N皇后问题
  • (一)u-boot-nand.bin的下载
  • (转)我也是一只IT小小鸟
  • .NET Core WebAPI中使用swagger版本控制,添加注释
  • .Net Memory Profiler的使用举例
  • .net 设置默认首页
  • .NetCore实践篇:分布式监控Zipkin持久化之殇
  • ::前边啥也没有