这几天一直接触ActionMode 的知识,这里就把自己接触到的知识点顺手记下来,以后肯定会用的找的
1.ActionMode的创建
private ActionMode.Callback mCallback = new ActionMode.Callback() { @Override public boolean onActionItemClicked(ActionMode arg0, MenuItem arg1) { final ArrayList<GridItem> selectItem = mAdapter.mSelectItem; if (arg1.getItemId() == R.id.menu_delete) { new AlertDialog.Builder(mContext) .setTitle(mContext.getString(R.string.delete)) .setIcon(null) .setMessage(mContext.getResources() .getQuantityString(R.plurals.delete_selection, mAdapter.mSelectItem.size())) .setPositiveButton(mContext.getString(R.string.ok), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { DeleteImage da = new DeleteImage(selectItem); da.execute(); } }) .setNegativeButton(mContext.getString(R.string.cancel), new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { } }).show(); } else if (arg1.getItemId() == R.id.menu_share) { Intent intent = computeSharingIntent(); getActivity().startActivity(Intent.createChooser(intent, getString(R.string.share))); } else if (arg1.getItemId() == R.id.menu_select) { if (selectItem.size() == mGirdList.size()) { clearGridList(); } else { selectAllGridList(); } updateSelectNumber(); mAdapter.notifyDataSetChanged(); } return false; } @Override public boolean onCreateActionMode(ActionMode arg0, Menu arg1) { LayoutInflater layoutInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);//添加普通的布局文件,大多是显示在顶上的---- View customView = layoutInflater.inflate(R.layout.funui_action_mode, null);//----->注意是LayoutInflater arg0.setCustomView(customView); mSelect = (TextView) customView.findViewById(R.id.select); MenuInflater inflater = arg0.getMenuInflater();//注意跟上面的区别,这个是用来添加menu的 inflater.inflate(R.menu.image_menu, arg1); //这里还可以用来设置里面的元素的状态,使用的方法是mSelectItem = menu.findItem(R.id.select_all); return true; } @Override public void onDestroyActionMode(ActionMode arg0) { setSelectMode(false); mAdapter.notifyDataSetChanged(); } @Override public boolean onPrepareActionMode(ActionMode arg0, Menu arg1) { return false; } };
解释
1.onActionItemClicked(),这个方法主要是用来定义ActionMode 中menu项点击事件的处理,MenuItem就是里面的元素,就像LinearLayout中的Button一样
2.onCreateActionMode(),这个方法主要是用来定义ActionMode中menu的显示,加载menu布局,如果想要隐藏其中的个别元素,也是在这里面处理的
自己想要添加一个全选功能,添加的代码如下
private void cancelSelect() { 266 for (Album album : mAlbumlist) { album.isSelected = false; } mAdapter.mSelectAlbum.clear(); } private void selectAll() { mAdapter.mSelectAlbum.clear(); for (Album album : mAlbumlist) { album.isSelected = true; mAdapter.mSelectAlbum.add(album); } }
其实这个就是找到里面存储选中的listview,对这个listview进行操作就行了
时间: 2024-10-21 18:43:15