Contextual Action Mode
实现ActionMode.Callback接口,在接口内配置ActionMode并响应action items的点击事件。在需要显示ActionMode时调用startSupportActionMode()方法。
两种基本的设计:
1.对个体任意views的上下文相关操作;
调用View的setOnLongClickListener方法。
实现OnLongClickListener接口,在接口内调用startSupportActionMode()方法。
2.对一组数据的批处理,比如ListView或GridView中的项目,允许用户选择多个项目然后对它们整体执行一个动作。
public class MainActivity extends ActionBarActivity { private ListView mListView = null; private String[] mStrings = { "a", "b", "c", "d" }; private ActionMode mActionMode = null; private ArrayAdapter<String> Adapter0, Adapter1; //设置ActionMode菜单 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Adapter0 = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mStrings); Adapter1 = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, mStrings); mListView = (ListView) findViewById(R.id.listView1); closeEditor(); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == R.id.action_edit) { if (mActionMode != null) { return false; } //startSupportActionMode mActionMode = startSupportActionMode(mActionModeCallback); return true; } return super.onOptionsItemSelected(item); } //ActionMode.Callback private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() { @Override public boolean onCreateActionMode(ActionMode mode, Menu menu) { MenuInflater inflater = mode.getMenuInflater(); inflater.inflate(R.menu.cab, menu); return true; } //预处理 @Override public boolean onPrepareActionMode(ActionMode mode, Menu menu) { //设置适配器 mListView.setAdapter(Adapter1); //设置监听器 mListView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { } }); //设置选择模式——多选 mListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); return true; } //点击处理 @Override public boolean onActionItemClicked(ActionMode mode, MenuItem item) { switch (item.getItemId()) { case R.id.action_multiple_choice: showEditor(); //关闭ContextualActionMode mode.finish(); return true; default: return false; } } //销毁处理 @Override public void onDestroyActionMode(ActionMode mode) { mActionMode = null; closeEditor(); } }; private void showEditor() { //得到选中的itemId long[] ids = mListView.getCheckItemIds(); String str = ""; for (int i = 0; i < ids.length; i++) { str += ids[i] + ","; } //输出ID Toast.makeText(MainActivity.this, str, Toast.LENGTH_LONG).show(); } private void closeEditor() { //设置适配器 mListView.setAdapter(Adapter0); //设置监听器 mListView.setOnItemClickListener(new OnItemClickListener() { // onItemClick @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { //得到选中的itemId String ids = id + ""; //输出ID Toast.makeText(MainActivity.this, ids, Toast.LENGTH_LONG) .show(); } }); //设置选择模式——单选 mListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE); } }
自定义样式:
使用方法同ActionBar相同,这里只给出样式属性。
"android:actionModeStyle "——ActionMode样式。
"android:actionModeBackground"——ActionMode背景。
"android:actionModeSplitBackground"——分割时底部背景(同ActionBar)。
"android:actionModeCloseButtonStyle"——取消按钮样式(就是那个对号)。
Android2.3模拟器效果图
相关推荐
Contextual action mode可以用来在程序运行时切换ActionBar的布局,下面将介绍如何使用
Android contextual action mode text android_cabTest
Contextual_Action_Bar 为了自定义操作栏,我们覆盖上下文操作栏
Contextual Multi-armed Bandits A Survey on Contextual Multi-armed Bandits.pdf
contextual embedding 综述 Contextual embeddings, such as ELMo and BERT, move beyond global word represen- tations like Word2Vec and achieve ground- breaking performance on a wide range of natu- ral ...
Contextual Bandits 算法在推荐场景中的应用源码
material cab允许您在应用程序中实现一个可定制且灵活的上下文操作栏。
Android结合Actionbar使用的上下文菜单,高亮显示选中状态的item,实现多选、反选功能
Multilevel Contextual 3-D CNNs for False Positive Reduction in Pulmonary Nodule Detection。分享给大家~
Identifying Encrypted Malware Traffic with Contextual Flow Data, ETA
Daniel Munoz关于马尔科夫随机场高阶能量优化的论文Contextual Classification with Functional Max-Margin Markov Networks源码
基于边界限制的去雾方法,与传统的暗原色不同,效果很好,大家可以和何凯明的方法对比
Contextual bandit learning is a reinforcement learning problemwhere the learner repeatedly receives a set of features (context), takes an action and receives a reward based on the action and context
介绍ADF Contextual Events的简单实用。 参见博客:http://blog.csdn.net/ygj26/article/details/24254607
The aim of this book is to show that the probabilistic formalisms of classical statistical mechanics and quantum mechanics can be unified on the basis of a general contextual probabilistic model....
ContextualAction ContextualActionWidget ContextualScaffold<?> ContextualScaffold<?>与普通材质Scaffold类似,除了它还需要一个必需的contextualAppBar 。 ContextualScaffold<?>( ...
前端开源库-contextual用于帮助应用主题和其他公共上下文属性的React应用程序的上下文和上下文生成器。
Contextual.js Javascript上下文菜单库-创建动态的右键单击或弹出菜单。 菜单 选项 isSticky 宽度 项目 菜单项 种类 子菜单 悬停菜单 分隔符 纽扣 自定义元素 多按钮(WIP) 选项 图标 cssIcon 标签 类型 onClick...
Learning Conceptual-Contextual Embeddings for Medical Text.pdf
Collaborative Contextual Anomaly Detection for Industrial Equipment Groups