复选框在网格视图持有者模式中显示多个时间

时间:2016-04-15 05:40:27

标签: android gridview checkbox android-viewholder

我正在显示和隐藏imageview上的复选框点击gridview但是当我点击一个imageView复选框出现在多个图像视图android和因为我滚动可见复选框丢失当我滚动 这是我的适配器

     public class ImageAdapter extends BaseAdapter {
        private LayoutInflater mInflater;

        public ImageAdapter() {
            mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }



        public int getCount() {
            return arrPath.length;
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(final int position, View convertView, ViewGroup parent) {
            final ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = mInflater.inflate(
                        R.layout.galleryitems, parent,false);
                holder.imageview = (ImageView) convertView.findViewById(R.id.thumbImage);
                holder.checkbox = (CheckBox) convertView.findViewById(R.id.itemCheckBox);
                convertView.setTag(R.id.itemCheckBox, holder.checkbox);
                convertView.setTag(R.id.thumbImage,holder.imageview);
                convertView.setTag(holder);




            } else {
                holder = (ViewHolder) convertView.getTag();
                holder.checkbox = (CheckBox)convertView.getTag();
                holder.imageview = (ImageView)convertView.getTag();
            }
            holder.checkbox.setId(position);
            holder.imageview.setId(position);
            holder.checkbox.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    CheckBox cb = (CheckBox) v;
                    int id = cb.getId();


                    if (thumbnailsselection[id]) {
                        cb.setChecked(false);
                        thumbnailsselection[id] = false;
                    } else {
                        cb.setChecked(true);
                        thumbnailsselection[id] = true;
                    }
                }
            });
            holder.imageview.setOnClickListener(new View.OnClickListener() {

                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    ImageView iv = (ImageView )v;
                    int id = iv.getId();
                    if (holder.checkbox.getVisibility() == View.GONE) {

                            holder.checkbox.getTag();
                            holder.imageview.getTag();
                            holder.imageview.setAlpha(0.22f);
                            holder.checkbox.setVisibility(View.VISIBLE);
                            holder.checkbox.setChecked(true);


                    } else if (holder.checkbox.getVisibility() == View.VISIBLE) {
                        holder.imageview.setAlpha(0.8f);
                        holder.checkbox.setVisibility(View.GONE);
                    }



                }
            });
            holder.checkbox.setTag(position);
            holder.imageview.setImageBitmap(thumbnails[position]);
            holder.checkbox.setChecked(thumbnailsselection[position]);
            holder.id = position;
            return convertView;
        }
 }

    class ViewHolder {
        ImageView imageview;
        CheckBox checkbox;
        int id;
    }
}

2 个答案:

答案 0 :(得分:1)

试试这个适合我的代码,

public class ImageAdapter extends BaseAdapter {
        private LayoutInflater mInflater;
        private ArrayList<Items> list
        public ImageAdapter(ArrayList<Items> list) {
            this.list=list;
            mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }



        public int getCount() {
            return list.size();
        }

        public Items getItem(int position) {
            return list.get(position);
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(final int position, View convertView, ViewGroup parent) {
            final ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = mInflater.inflate(
                        R.layout.galleryitems, parent,false);
                holder.imageview = (ImageView) convertView.findViewById(R.id.thumbImage);
            //Take image view in your XML       
                holder.checkbox = (ImageView) convertView.findViewById(R.id.itemCheckBox);

                convertView.setTag(holder);




            } else {
                holder = (ViewHolder) convertView.getTag();

            }

            if(list.get(position).isImageSelected()){
                 // set your check image 
                 holder.checkbox.setImageResource(checkResId);
            }else{
                 // set your Uncheck image 
                 holder.checkbox.setImageResource(unCheckResId);
            }

            holder.checkbox.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {



                    if(list.get(position).isImageSelected()){
                       // set your Uncheck image 
                        holder.checkbox.setImageResource(unCheckResId);
                        list.get(position).setIsImageSelected(false);
                    } else {
                        // set your check image 
                         holder.checkbox.setImageResource(checkResId); 
                        list.get(position).setIsImageSelected(false);
                    }
                  notifyDataSetChanged();
                }
            });

            return convertView;
        }
 }

    class ViewHolder {
        ImageView imageview;
        // Use ImageView for select and deselect
        ImageView checkbox;
        int id;
    }
}

public class Items {



    String thumbImageUrl;
    boolean isImageSelected;



    public String getThumbImageUrl() {
        return thumbImageUrl;
    }

    public void setThumbImageUrl(String thumbImageUrl) {
        this.thumbImageUrl = thumbImageUrl;
    }

    public boolean isImageSelected() {
        return isImageSelected;
    }

    public void setIsImageSelected(boolean isImageSelected) {
        this.isImageSelected = isImageSelected;
    }
}

答案 1 :(得分:0)

这不是你使用ViewHolder的方式。 当您将它们设置为标记时,ViewHolder也将像convertView一样重复使用。 它应该链接UI,因此您不需要再次调用findViewById。 但是您有责任维护数据(您在案例中选择的ImageView的位置)

this为例。