In project development, you need to use the gridview batch processing operation and select multiple options.
However, gridview. setchoicemode (gridview. choice_mode_multiple_modal) is not compatible with earlier versions.
Find a solution and view the options of multiple options of the gridview in the demo provided by the android SDK.
public class Grid3 extends Activity { GridView mGrid; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); loadApps(); setContentView(R.layout.grid_1); mGrid = (GridView) findViewById(R.id.myGrid); mGrid.setAdapter(new AppsAdapter()); mGrid.setChoiceMode(GridView.CHOICE_MODE_MULTIPLE_MODAL); mGrid.setMultiChoiceModeListener(new MultiChoiceModeListener()); } private List<ResolveInfo> mApps; private void loadApps() { Intent mainIntent = new Intent(Intent.ACTION_MAIN, null); mainIntent.addCategory(Intent.CATEGORY_LAUNCHER); mApps = getPackageManager().queryIntentActivities(mainIntent, 0); } public class AppsAdapter extends BaseAdapter { public AppsAdapter() { } public View getView(int position, View convertView, ViewGroup parent) { CheckableLayout l; ImageView i; if (convertView == null) { i = new ImageView(Grid3.this); i.setScaleType(ImageView.ScaleType.FIT_CENTER); i.setLayoutParams(new ViewGroup.LayoutParams(50, 50)); l = new CheckableLayout(Grid3.this); l.setLayoutParams(new GridView.LayoutParams(GridView.LayoutParams.WRAP_CONTENT, GridView.LayoutParams.WRAP_CONTENT)); l.addView(i); } else { l = (CheckableLayout) convertView; i = (ImageView) l.getChildAt(0); } ResolveInfo info = mApps.get(position); i.setImageDrawable(info.activityInfo.loadIcon(getPackageManager())); return l; } 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 { public boolean onCreateActionMode(ActionMode mode, Menu menu) { mode.setTitle("Select Items"); mode.setSubtitle("One 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) { } public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) { int selectCount = mGrid.getCheckedItemCount(); switch (selectCount) { case 1: mode.setSubtitle("One item selected"); break; default: mode.setSubtitle("" + selectCount + " items selected"); break; } } }}
Implements implements checkable, And you can set multiple options for the gridview, but it is not compatible with earlier versions.
Later, I learned how to solve the problem of compatibility with lower versions. For details, refer to the code.
Adapter set for the gridview
List<CodeGoodsListEntiy> goods = new ArrayList<CodeGoodsListEntiy>();public class CollectionAdapter extends BaseAdapter{@Overridepublic int getCount() {// TODO Auto-generated method stubreturn goods.size();}@Overridepublic Object getItem(int position) {// TODO Auto-generated method stubreturn position;}@Overridepublic long getItemId(int position) {// TODO Auto-generated method stubreturn position;}public void addDataList(List<CodeGoodsListEntiy> goodsList){goods.addAll(goodsList);}public void removeAllList(){goods.removeAll(goods);}public void removeData(CodeGoodsListEntiy entity){goods.remove(entity);}public List<CodeGoodsListEntiy> getList(){return goods;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {// TODO Auto-generated method stubCheckableLayout layout = null;CodeGoodsListEntiy good = goods.get(position);if(convertView==null){layout = new CheckableLayout();convertView = inflater.inflate(R.layout.mycollection_griditem, null);layout.imgviewlayout = (RelativeLayout) convertView.findViewById(R.id.imgviewlayout);layout.iv = (ImageView) convertView.findViewById(R.id.iv);layout.price = (TextView) convertView.findViewById(R.id.price);layout.title = (TextView) convertView.findViewById(R.id.title);layout.sellnums = (TextView) convertView.findViewById(R.id.sells);layout.item_selectbtn = (ImageView) convertView.findViewById(R.id.item_selectbtn);layout.item_topview = (ImageView) convertView.findViewById(R.id.item_topview);convertView.setTag(layout);}else{layout = (CheckableLayout) convertView.getTag();}layout.price.setText("¥"+good.getSalePrice());layout.title.setText(good.getTitle());layout.sellnums.setText("月销量"+good.getSellNums());layout.imgviewlayout.getLayoutParams().height = (SettingUtil.getDisplaywidthPixels() - SettingUtil.dip2px(8)) / 2;layout.iv.getLayoutParams().height = (SettingUtil.getDisplaywidthPixels() - SettingUtil.dip2px(10)) / 2;App.imageLoader.displayImage(good.getMainImg(), layout.iv, options, new SimpleImageLoadingListener());if(editModel){if(selecteditem.contains(position)){layout.setChecked(true);}else{layout.setChecked(false);}}else{layout.item_selectbtn.setVisibility(View.GONE);layout.item_topview.setVisibility(View.GONE);}return convertView;}}class CheckableLayout implements Checkable{View convertView;ImageView item_selectbtn;ImageView item_topview;ImageView iv;TextView price;TextView title;TextView sellnums;RelativeLayout imgviewlayout;private boolean mChecked = false;public CheckableLayout() {// TODO Auto-generated constructor stub}@Overridepublic void setChecked(boolean checked) {// TODO Auto-generated method stubmChecked = checked;if(item_topview!=null){if(mChecked){item_topview.setVisibility(View.GONE);}else{item_topview.setVisibility(View.VISIBLE);}}if(item_selectbtn!=null){if(mChecked){item_selectbtn.setVisibility(View.VISIBLE);}else{item_selectbtn.setVisibility(View.GONE);}}}@Overridepublic boolean isChecked() {// TODO Auto-generated method stubreturn mChecked;}@Overridepublic void toggle() {// TODO Auto-generated method stubsetChecked(!mChecked);}}
In fact, the class checkablelayout implements checkable is similar to the viewholder class in a general adapter.
The itemclicklistener method set for the gridview
private OnItemClickListener onlvItemClick = new OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {if(editModel){CheckableLayout holder = (CheckableLayout) arg1.getTag();if(selecteditem.contains(arg2)){selecteditem.remove((Integer)arg2);holder.setChecked(false);}else{selecteditem.add(arg2);holder.setChecked(true);}}else{ProDetailActivity.launch(MyCollectionActivity.this, goods.get(arg2).getItemId());}}};
In fact, the key here is to get the viewholder class through the sent view.
Checkablelayout holder = (checkablelayout) arg1.gettag ();
Let's talk about it here. Many project codes involve commercialization and can be shared if you have any questions. This method requires attention to the details.
Copy the Translation results to Google
Gridview. setchoicemode (gridview. choice_mode_multiple_modal) is not compatible with earlier version system solutions