原帖地址:http://blog.csdn.net/mayingcai1987/article/details/6232857
一. 概述:
HoneyComb3.0中支持ListView和GridView的多选操作(长按后进入ActionMode模式),并且可以对选中的ListView或GridView的条目
(Item)进行操作,比如:删除,修改等。
二. 效果图:
1. ListView选择效果:
3. GridView选择效果:
三. 代码实现:
1. ListView多选,见效果图:
(1)在res/menu/目标中创建菜单文件:
- <?xml version="1.0" encoding="utf-8"?>
- <menu xmlns:android="http://schemas.android.com/apk/res/android">
- <item android:id="@+id/share"
- android:title="share"
- android:icon="@android:drawable/ic_menu_share"
- android:showAsAction="always"
- />
- </menu>
(2)主Activity实现:
- package com.focus.multichoice;
- import android.app.ListActivity;
- import android.os.Bundle;
- import android.view.ActionMode;
- import android.view.Menu;
- import android.view.MenuInflater;
- import android.view.MenuItem;
- import android.widget.ArrayAdapter;
- import android.widget.ListView;
- import android.widget.Toast;
- public class ListViewActivity extends ListActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- ListView mListView = getListView();
- /**
- * 设置多选模式。
- */
- mListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
- /**
- * 设置多选 模式监听器。
- */
- mListView.setMultiChoiceModeListener(new MultiChoiceModeCallback());
- /**
- * 渲染ListView。
- */
- setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_checked, mStrings));
- }
- /**
- * 在onStart()方法调用完成后会调用onPostCreate()方法,为ActionBar设置子标题,即:二级标题。
- */
- @Override
- protected void onPostCreate(Bundle savedInstanceState) {
- super.onPostCreate(savedInstanceState);
- getActionBar().setSubtitle("长按开始选择");
- }
- private class MultiChoiceModeCallback implements ListView.MultiChoiceModeListener {
- /**
- * 创建ActionMode,即:长按ListView以后会出现蒙板效果,这个蒙板就是ActionMode,
- * 可以在上面添加菜单,快捷按钮等。
- */
- public boolean onCreateActionMode(ActionMode mActionMode, Menu menu) {
- MenuInflater inflater = getMenuInflater();
- /**
- * 设置ActionMode的菜单。
- */
- inflater.inflate(R.menu.list_select_menu, menu);
- /**
- * 设置ActionMode的标题。
- */
- mActionMode.setTitle("Select Items");
- setSubtitle(mActionMode);
- return true;
- }
- public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
- return true;
- }
- /**
- * 单击ActionMode的菜单项会调用此方法。
- */
- public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
- switch (item.getItemId()) {
- case R.id.share:/* menu条目 */
- Toast.makeText(ListViewActivity.this, "Shared " + getListView().getCheckedItemCount() + " items", Toast.LENGTH_SHORT).show();
- mode.finish();
- break;
- default:
- Toast.makeText(ListViewActivity.this, "Clicked " + item.getTitle(), Toast.LENGTH_SHORT).show();
- break;
- }
- return true;
- }
- public void onDestroyActionMode(ActionMode mode) {
- }
- /**
- * 在选择ListView的一个条目时会触发。
- */
- public void onItemCheckedStateChanged(ActionMode mActionMode, int position, long id, boolean checked) {
- setSubtitle(mActionMode);
- }
- private void setSubtitle(ActionMode mode) {
- final int checkedCount = getListView().getCheckedItemCount();
- switch (checkedCount) {
- case 0:
- mode.setSubtitle(null);
- break;
- case 1:
- mode.setSubtitle("1 item selected");
- break;
- default:
- mode.setSubtitle(checkedCount + " items selected");
- break;
- }
- }
- }
- private String [] mStrings = {"Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam"};
- }
2. GridView多选,见效果图:
(1)res/layout/main.xml实现:
- <?xml version="1.0" encoding="utf-8"?>
- <GridView
- xmlns:android = "http://schemas.android.com/apk/res/android"
- android:id = "@+id/mGridView"
- android:layout_width = "match_parent"
- android:layout_height = "match_parent"
- android:padding = "10dp"
- android:verticalSpacing = "10dp"
- android:horizontalSpacing = "10dp"
- android:numColumns = "auto_fit"
- android:columnWidth = "60dp"
- android:stretchMode = "columnWidth"
- android:gravity = "center"
- />
(2)主Activity实现:
- package com.focus.multichoice;
- import java.util.List;
- import android.app.Activity;
- import android.content.Context;
- import android.content.Intent;
- import android.content.pm.ResolveInfo;
- import android.os.Bundle;
- import android.view.ActionMode;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.BaseAdapter;
- import android.widget.Checkable;
- import android.widget.FrameLayout;
- import android.widget.GridView;
- import android.widget.ImageView;
- public class GridActivity extends Activity {
- private GridView mGridView;
- private List<ResolveInfo> mApps;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- loadAllApps();
- setContentView(R.layout.grid_layout);
- mGridView = (GridView) findViewById(R.id.mGridView);
- mGridView.setAdapter(new AppsAdapter());
- /**
- * 设置多选模式。
- */
- mGridView.setChoiceMode(GridView.CHOICE_MODE_MULTIPLE_MODAL);
- /**
- * 设置多选 模式监听器。
- */
- mGridView.setMultiChoiceModeListener(new MultiChoiceModeListener());
- }
- /**
- * 通过PackageManager查找所有应用程序。
- */
- private void loadAllApps() {
- Intent mMainIntent = new Intent(Intent.ACTION_MAIN, null);
- mMainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
- mApps = getPackageManager().queryIntentActivities(mMainIntent, 0);
- }
- /**
- *
- * 渲染GridView。
- *
- * @author mayingcai
- *
- */
- public class AppsAdapter extends BaseAdapter {
- public View getView(int position, View convertView, ViewGroup parent) {
- CheckableLayout mCheckableLayout;
- ImageView mImageView;
- if (convertView == null) {
- mImageView = new ImageView(GridActivity.this);
- mImageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
- mImageView.setLayoutParams(new ViewGroup.LayoutParams(50, 50));
- mCheckableLayout = new CheckableLayout(GridActivity.this);
- mCheckableLayout.setLayoutParams(new GridView.LayoutParams(GridView.LayoutParams.WRAP_CONTENT, GridView.LayoutParams.WRAP_CONTENT));
- mCheckableLayout.addView(mImageView);
- } else {
- mCheckableLayout = (CheckableLayout) convertView;
- mImageView = (ImageView) mCheckableLayout.getChildAt(0);
- }
- ResolveInfo info = mApps.get(position);
- mImageView.setImageDrawable(info.activityInfo.loadIcon(getPackageManager()));
- return mCheckableLayout;
- }
- public final int getCount() {
- return mApps.size();
- }
- public final Object getItem(int position) {
- return mApps.get(position);
- }
- public final long getItemId(int position) {
- return position;
- }
- }
- public class CheckableLayout extends FrameLayout implements Checkable {
- private boolean mChecked;
- public CheckableLayout(Context context) {
- super(context);
- }
- /**
- * 在被选择时设置蓝色背景。
- */
- public void setChecked(boolean checked) {
- mChecked = checked;
- setBackgroundDrawable(checked ? getResources().getDrawable(R.drawable.blue) : null);
- }
- public boolean isChecked() {
- return mChecked;
- }
- public void toggle() {
- setChecked( ! mChecked);
- }
- }
- public class MultiChoiceModeListener implements GridView.MultiChoiceModeListener {
- /**
- * 创建ActionMode蒙板。
- */
- public boolean onCreateActionMode(ActionMode mode, Menu menu) {
- mode.setTitle("Select Items");
- mode.setSubtitle("1 item selected");
- return true;
- }
- public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
- return true;
- }
- public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
- return true;
- }
- public void onDestroyActionMode(ActionMode mode) {
- }
- /**
- * 设置ActionMode标题。
- */
- public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
- int selectCount = mGridView.getCheckedItemCount();
- switch ( selectCount ) {
- case 1 :
- mode.setSubtitle("1 item selected");
- break;
- default :
- mode.setSubtitle("" + selectCount + " items selected");
- break;
- }
- }
- }
- }