Contextual Action Mode

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"——取消按钮样式(就是那个对号)。


Contextual Action Mode

 Android2.3模拟器效果图

你可能感兴趣的:(action,mode,Contextual)