ActionMode 操作模式

来源:互联网 发布:新淘宝店铺转让价格表 编辑:程序博客网 时间:2024/05/17 21:51

最近在修改记事本模块看到这个东西的以前没怎么用过

使用ActionMode,它会临时占据action bar的位置,但与ActionBar是独立的。
学习的情况是 点击涂鸦按钮,进入涂鸦界面。进行分享,保存等其他操作

当用户取消了所有选择的项目、按下BACK按钮、或选择操作栏左边的“执行”操作时,这种操 作模式就会被禁止,并且上下文操作栏也会被隐藏。

开启 actionmode 模式

// 这里在start时, 也是可以得到 ActionMode的ActionMode action = mActivity.startActionMode(new SketchModeCallBack());

关闭 actionmode 模式, 一种是点返回键,另外一种是代码控制

这里写代码片

mActionMode.finish();

 private ActionMode mActionMode;    public  class SketchModeCallBack implements ActionMode.Callback, OnClickListener {        private ImageView mColor;        private ImageView mWidth;        public boolean onCreateActionMode(ActionMode mode, Menu menu) {            //可自定义最上层显示的view            View v = LayoutInflater.from(mActivity).inflate(                    R.layout.sketch_edit, null);            mColor = (ImageView) v.findViewById(R.id.sketch_color);            mRecover.setOnClickListener(this);            mode.setCustomView(v);            mActionMode = mode;            enterSketchMode();            //mActivity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NEEDS_MENU_KEY); //lgj sdk5.1 not supprot            mBackPressed = false;            return true;        }        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {            // TODO Auto-generated method stub            return true;        }        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {            boolean ret = false;            return ret;        }        public void onDestroyActionMode(ActionMode mode) {            dismissPopupWindow();            exitSketchMode();            //mActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_NEEDS_MENU_KEY);//lgj sdk5.1 not supprot            mActionMode = null;            if (!mBackPressed)                savePageSketch();        }private void enterSketchMode() {        hideInputMethod();        setSketchState(true);    }    public void setSketchState(boolean b) {        mSketcher.setSketchState(b);        // 开启画板模式,设定surfaceview显示 visibily        if (b) {            mSketcher.clearBoard();            mSketcher.setVisibility(View.VISIBLE);//           mCover.setVisibility(View.GONE);        } else {            mSketcher.setVisibility(View.GONE);//            mCover.setVisibility(View.VISIBLE);        }    }

1.创建
普通的例子

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_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.    @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.menu_share:                shareCurrentItem();                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;    }};

当你调用startActionMode()方法时,系统返回被创建的ActionMode对象。通过把这个对象保存在一个成员变量中,你能够在对 其他事件的响应中对上下文菜单进行改变。在上例中,在启动这种操作模式之前,通过检查ActionMode对象的实例是否为null,来确保这个对象实例 不被重复创建。

启用 actionmode 模式

someView.setOnLongClickListener(new View.OnLongClickListener() {    // Called when the user long-clicks on someView    public 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;    }});

3.实例

3.1
在ListView或GridView对象中的批处理上下文操作,允许用户对其执行批处理操作,1. 实现AbsListView.MultiChoiceModeListener接口,并且要用 setMultiChoiceModeListener()方法把它设置给ViewGroup对象。在这个监听器的回调方法中,你能够给这个上下文操作栏 指定动作,响应在操作项目上的点击事件,并且处理从ActionMode.Callback接口中继承来的其他回调方法。

  1. 调用带有CHOICE_MODE_MULTIPLE_MODAL参数的setChoiceMode()方法。
ListView listView = getListView();listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);listView.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.menu_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, 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;    }});

在某些场景中,上下文操作会给一些项目提供共通的操作,因此你可能想要添加复选框或类似的允许用户选择项目的UI元素,因为它们可能没有长按事件行 为,所以在用户选择复选框时,你能够通过设置各自列表项的复选状态的setItemChecked()方法来调用上下文操作模式。

1 0