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翻译翻译结果

时间: 2024-10-17 10:26:08

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 savedInsta

低版本系统兼容的ActionBar(二)ActionProvider+分离式ActionBar+分离式的ActionMode

       这篇文章主要讲的是在低版本兼容的ActionBar中实现自定义的ActionProvider,ShareActionProvider的使用方法,如何实现分离式ActionBar,外加在分离式ActionBar上的ActionMode的效果. 一.自定义ActionProvider 建立一个类,继承android.support.v4.view.ActionProvider,然后复写里面的方法即可.主要就是初始化视图和相应点击事件. 范例一: SettingsActionProvid

android api实现高斯模糊,且兼容低版本

一.利用android api实现高斯模糊 关于高斯模糊网上已经有很多不错的文章介绍了,在这里就不重复了.先来看一个效果 效果已经看到了,就来看看怎么实现的吧.首先是实现高斯模糊的核心代码 <span style="white-space:pre"> </span>public static Bitmap blurBitmap(Bitmap bitmap, Context context) { // 用需要创建高斯模糊bitmap创建一个空的bitmap Bit

兼容低版本的array_column

//兼容低版本的array_column public function array_column($input, $columnKey, $indexKey = NULL) { $columnKeyIsNumber = (is_numeric($columnKey)) ? TRUE : FALSE; $indexKeyIsNull = (is_null($indexKey)) ? TRUE : FALSE; $indexKeyIsNumber = (is_numeric($indexKey))

低版本系统兼容的ActionBar(三)自定义Item视图+进度条的实现+下拉导航+透明ActionBar

       一.自定义MenuItem的视图 custom_view.xml (就是一个单选按钮) <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android

模拟实现兼容低版本IE浏览器的原生bind()函数功能

模拟实现兼容低版本IE浏览器的原生bind()函数功能: 代码如下: if(!Function.prototype.bind){   Function.prototype.bind=function(oThis){     if (typeof this !== 'function'){       throw new TypeError('调用者不是当前函数对象');     }       var aArgs = Array.prototype.slice.call(arguments, 1

低版本系统兼容的ActionBar(四)添加Tab+添加自定义的Tab视图

在ActionBar中添加Tab是很有用的技巧.在support V7库的支持下,我们几乎可以用和之前一样的方式来添加Tab,对于Tab来说,我们可以和MenuItem一样,给他定义自己的视图.我这里定义了个进度条,主要是来说明这个问题.本系列的博文也将接近尾声了,后面就是个定义ActionBar样式和一个仿照微信的实例,顺便讲解下Fragment和ViewPager的用法. activity_main.xml(定义了一个Fragment) <RelativeLayout xmlns:andro

低版本系统兼容的ActionBar(六)用Fragment+ViewPager+Tab实现快速导航

Tab经常和Fragment结合使用,这一讲我们用3种方式来实现这种快捷导航. 0.重要的两个监听器 MyTabListener,这个我们之前已经接触过了 package com.kale.actionbar05; import android.support.v4.app.FragmentTransaction; import android.support.v4.view.ViewPager; import android.support.v7.app.ActionBar; import a

兼容低版本IE浏览器的边框阴影效果

兼容低版本IE浏览器的边框阴影效果:使用box-show属性可以实现边框阴影效果,但是IE8和IE8以下浏览器不支持此属性,不过可以通过以下方式实现,当然实现全兼容的方式有多种,下面是一种比较简陋的方式,代码实例如下: <!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <meta name="author" content="http://www