ActionBar actionMode

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


private ListView mSizeOrderedListView;

 mSizeOrderedListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);

 ModeCallback modeCallback = new ModeCallback(this);

 mSizeOrderedListView.setMultiChoiceModeListener(modeCallback);


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

        public ModeCallback(DownloadList downloadList) {
            mDownloadList = downloadList;
        }

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

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

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

        @Override
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
            if (mDownloadList.mSelectedIds.size() == 0) {
                // nothing selected.
                return true;
            }
            switch (item.getItemId()) {
                case R.id.delete_download:
                    for (Long downloadId : mDownloadList.mSelectedIds.keySet()) {
                        mDownloadList.deleteDownload(downloadId);
                    }
                    // 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);
                        }
                    }
                    mDownloadList.mSelectedIds.clear();
                    // update the subtitle
                    onItemCheckedStateChanged(mode, 1, 0, false);
                    break;
                case R.id.share_download:
                    mDownloadList.shareDownloadedFiles();
                    break;
            }
            return true;
        }

        @Override
        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);
                    return;
                }
            }
            mDownloadList.setActionModeTitle(mode);
        }
    }





AbsListView------MultiChoiceModeListener

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


ActionMode------Callback


    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是独立的。

ActionBar的主要目的:

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

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

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

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

ActionBar设计模式:http://developer.android.com/design/patterns/actionbar.html



你可能感兴趣的:(ListView,action,callback,download,interface,menu)