在网格视图持有者模式中显示多次的复选框

Checkbox showing multiple time in grid view holder Pattern

我在网格视图中显示和隐藏 imageview 上的复选框单击,但是当我单击一个 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;
    }
}

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

this为例。

试试这个对我有用的代码,

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;
    }
}