ActionBar actionMode

原创 2012年03月26日 10:01:43

DownloadProvider-----DownloadList         多选----分享 , 删除

private ListView mSizeOrderedListView;


 ModeCallback modeCallback = new ModeCallback(this);


    private static class ModeCallback implements MultiChoiceModeListener {
        private final DownloadList mDownloadList;

        public ModeCallback(DownloadList downloadList) {
            mDownloadList = downloadList;

        @Override public void onDestroyActionMode(ActionMode mode) {
            mDownloadList.mActionMode = null;

        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            return true;

        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            if (mDownloadList.haveCursors()) {
                final MenuInflater inflater = mDownloadList.getMenuInflater();
                inflater.inflate(, menu);
            mDownloadList.mActionMode = mode;
            return true;

        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
            if (mDownloadList.mSelectedIds.size() == 0) {
                // nothing selected.
                return true;
            switch (item.getItemId()) {
                    for (Long downloadId : mDownloadList.mSelectedIds.keySet()) {
                    // uncheck all checked items
                    ListView lv = mDownloadList.getCurrentView();
                    SparseBooleanArray checkedPositionList = lv.getCheckedItemPositions();
                    int checkedPositionListSize = checkedPositionList.size();
                    ArrayList<DownloadItem> sharedFiles = null;
                    for (int i = 0; i < checkedPositionListSize; i++) {
                        int position = checkedPositionList.keyAt(i);
                        if (checkedPositionList.get(position, false)) {
                            lv.setItemChecked(position, false);
                            onItemCheckedStateChanged(mode, position, 0, false);
                    // update the subtitle
                    onItemCheckedStateChanged(mode, 1, 0, false);
            return true;

        public void onItemCheckedStateChanged(ActionMode mode, int position, long id,
                boolean checked) {
            // ignore long clicks on groups
            if (mDownloadList.isCurrentViewExpandableListView()) {
                ExpandableListView ev = mDownloadList.getExpandableListView();
                long pos = ev.getExpandableListPosition(position);
                if (checked && (ExpandableListView.getPackedPositionType(pos) ==
                        ExpandableListView.PACKED_POSITION_TYPE_GROUP)) {
                    // ignore this click
                    ev.setItemChecked(position, false);


public interface MultiChoiceModeListener extends ActionMode.Callback {
         * Called when an item is checked or unchecked during selection mode.
         * @param mode The {@link ActionMode} providing the selection mode
         * @param position Adapter position of the item that was checked or unchecked
         * @param id Adapter ID of the item that was checked or unchecked
         * @param checked <code>true</code> if the item is now checked, <code>false</code>
         *                if the item is now unchecked.
        public void onItemCheckedStateChanged(ActionMode mode,
                int position, long id, boolean checked);


    public interface Callback {
         * Called when action mode is first created. The menu supplied will be used to
         * generate action buttons for the action mode.
         * @param mode ActionMode being created
         * @param menu Menu used to populate action buttons
         * @return true if the action mode should be created, false if entering this
         *              mode should be aborted.
        public boolean onCreateActionMode(ActionMode mode, Menu menu);

         * Called to refresh an action mode's action menu whenever it is invalidated.
         * @param mode ActionMode being prepared
         * @param menu Menu used to populate action buttons
         * @return true if the menu or action mode was updated, false otherwise.
        public boolean onPrepareActionMode(ActionMode mode, Menu menu);

         * Called to report a user click on an action button.
         * @param mode The current ActionMode
         * @param item The item that was clicked
         * @return true if this callback handled the event, false if the standard MenuItem
         *          invocation should continue.
        public boolean onActionItemClicked(ActionMode mode, MenuItem item);

         * Called when an action mode is about to be exited and destroyed.
         * @param mode The current ActionMode being destroyed
        public void onDestroyActionMode(ActionMode mode);

ActionMode    Since: API Level 11

 * Represents a contextual mode of the user interface. Action modes can be used for
 * modal interactions with content and replace parts of the normal UI until finished.
 * Examples of good action modes include selection modes, search, content editing, etc.

ActionBar      Since: API Level 11

Class Overview

A window feature at the top of the activity that may display the activity title, navigation modes, and other interactive items.

Beginning with Android 3.0 (API level 11), the action bar appears at the top of an activity's window when the activity uses the system'sHolo theme (or one of its descendant themes), which is the default. You may otherwise add the action bar by callingrequestFeature(FEATURE_ACTION_BAR) or by declaring it in a custom theme with thewindowActionBar property.

By default, the action bar shows the application icon on the left, followed by the activity title. If your activity has an options menu, you can make select items accessible directly from the action bar as "action items". You can also modify various characteristics of the action bar or remove it completely.

From your activity, you can retrieve an instance of ActionBar by callinggetActionBar().

In some cases, the action bar may be overlayed by another bar that enables contextual actions, using anActionMode. For example, when the user selects one or more items in your activity, you can enable an action mode that offers actions specific to the selected items, with a UI that temporarily replaces the action bar. Although the UI may occupy the same space, the ActionMode APIs are distinct and independent from those forActionBar.

Android 3.0 (API level11)增加的特性。是一种窗口特性,标识了应用程序,提供user actions与导航modes,显示每个画面的上方,默认的系统themeHolo,都会有Action Bar存在。在menu中,提到了contextual action mode,使用ActionMode,它会临时占据action bar的位置,但与ActionBar是独立的。


1、 使一些重要的actions能够显著的显示与访问

2、 apps内,提供了一致性的导航与view间的切换

3、 很少使用的actions,提供了action overflow,画面不会凌乱

4、 为标识app提供了位置空间







  • 2015-02-23 11:22
  • 1.81MB
  • 下载

android ActionMode Test

  • 2013-04-16 15:32
  • 941KB
  • 下载

Android学习 - Android新的menu实现ActionMode

Android的menu有多种实现方式,这里介绍一种新的menu实现方式:ActionMode。ActionMode是Android 3.0以后出现的,我们可以使用AppCompat库使ActionM...


ActionBar因为一些原因切换到ActionMode。 启动ActionMode的方式:

android EditText长按屏蔽ActionMode context菜单但保留选择工具功能

最近项目要求屏蔽EditText 长按出来的ActionMode菜单,但是要保留选择文本功能。这个屏蔽百度会出现各种方法,这里说一下我的思路: 1.屏蔽百度可知setCustomSelectio...


Android的menu有多种实现方式,以前写过一篇Android中五种常用的menu(菜单),这里介绍一种新的menu实现方式:ActionMode。ActionMode是Android 3.0...


在6.0奇葩问题中,我已经对此问题分析过了,但是并没有一个很好的解决方案,虽然开了开发版本,但是导致对于的开发版本已经会出现长按报null问题! 最近在查看源码时发现,在分析ActionBarCo...

Android--Menu的实现 ActionMode

Android的menu有多种实现方式,尤其是当处理列表的长按事件时经常使用ContextMenu,自从Android3.0后还可以使用ActionMode。 两者所呈现出来的效果有所不同,左图为C...

Android Toolbar使用ActionMode

Toolbar使用ActionMode 基本情况 设置Style Java 总结 谢谢基本情况ActionMode 其实是一种菜单方式,从Android3.0之后出现,可以通过AppCompat兼容老...