首页 > 代码库 > GridView.setChoiceMode(GridView.CHOICE_MODE_MULTIPLE_MODAL)不兼容低版本系统解决方案

GridView.setChoiceMode(GridView.CHOICE_MODE_MULTIPLE_MODAL)不兼容低版本系统解决方案

项目开发中需要使用GridView批处理操作,多项选择。

但是GridView.setChoiceMode(GridView.CHOICE_MODE_MULTIPLE_MODAL)不兼容低版本。


找解决方案,查看android sdk提供的demo里介绍的GridView多项选择的方法

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 Checkable,然后可以设置GridView的多项选择,但是无法兼容低版本

后来自己琢磨发现了解决兼容低版本的方法,见代码

给GridView设置的Adapter

List<CodeGoodsListEntiy> goods = new ArrayList<CodeGoodsListEntiy>();
	public class CollectionAdapter extends BaseAdapter{

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return goods.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 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;
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			CheckableLayout 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
		}
		

		@Override
		public void setChecked(boolean checked) {
			// TODO Auto-generated method stub
			mChecked = 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);
				}
			}
		}

		@Override
		public boolean isChecked() {
			// TODO Auto-generated method stub
			return mChecked;
		}

		@Override
		public void toggle() {
			// TODO Auto-generated method stub
			setChecked(!mChecked);
		}

	}

其实这里class CheckableLayout implements Checkable类似于一般的Adapter里的ViewHolder的类一样的。

然后给GridView设置的ItemClickListener的方法

private OnItemClickListener onlvItemClick = new OnItemClickListener() {
		@Override
		public 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());
			}
		}
	};

其实这里很关键的就是通过传来的View得到那个ViewHolder类

CheckableLayout holder = (CheckableLayout) arg1.getTag();

讲到这里吧,很多项目代码涉及到商业性,有问题可以交流。这种方法需要注意细节就是了。

复制去Google翻译翻译结果

GridView.setChoiceMode(GridView.CHOICE_MODE_MULTIPLE_MODAL)不兼容低版本系统解决方案