如何从Android Gridview中删除复选框

时间:2015-12-07 11:18:03

标签: android

我正在实现删除Android网格视图中的图像复选框。我已经完成删除已选中的图像但是复选框显示为原样。如何从网格中删除复选框。可以帮助我解决此问题的复选框 这是我的适配器类

中的getView()方法

*

@Override
        public View getView(final int position, View convertView, ViewGroup parent)
        {
           ViewHolder holder;
            if (convertView == null)
            {
                holder = new ViewHolder();
                convertView = mInflater.inflate(R.layout.add_post_grid_item_layout, parent , false);
                holder.image = (ImageView) convertView.findViewById(R.id.image);
                holder.checkbox = (CheckBox) convertView.findViewById(R.id.itemCheckBox);
                convertView.setTag(holder);
            }
            else
            {
                holder = (ViewHolder) convertView.getTag();
            }
            Bitmap myBitmap = BitmapFactory.decodeFile(fileName.get(position));
            holder.image.setImageBitmap(myBitmap);
            holder.checkbox.setTag(position);
            holder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    mSparseBooleanArray.put((Integer) buttonView.getTag(), isChecked);
                    int pos = (Integer) buttonView.getTag();
                    if (!buttonView.isChecked()) {
                        picsName.remove((String) fileName.get(pos));
                    } else if (buttonView.isChecked()) {
                        if (!picsName.contains((String) fileName.get(pos))) {
                            picsName.add((String) fileName.get(pos));
                        }
                    }
                    notifyDataSetChanged();
                }
            });
            return convertView;
        }

Activity类

中的删除按钮
 imgDeleteImagesFromGallery.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    int len = selectedItems.size() - 1;
                    for (int i = len; i > 1; i--)
                    {
                        fileName.remove(i);
                        fileName.remove((Integer) vh.checkbox.getTag(i));
                        if(checks.get(i)==1){
                            checks.remove(i);
                            i--;
                        }
                    }
                    gridAdapter.notifyDataSetChanged();
                    gridView.clearChoices();
                }
            });

这是行xml文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="1dp"
    android:background="@android:color/transparent">

    <ImageView
        android:id="@+id/image"
        android:layout_width="115dp"
        android:layout_height="110dp"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:scaleType="fitXY" />

    <CheckBox
        android:id="@+id/itemCheckBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true" />


</RelativeLayout>

1 个答案:

答案 0 :(得分:1)

初始化selectedPic数​​组以存储在适配器

上选择的图像
private ArrayList<Integer> selectedPic = new ArrayList<Integer>();

创建此方法以跟踪所选的适配器类

上的复选框poisitions
public ArrayList<Integer> getSelectedChecckedImages() {
        return selectedPic;
    }

    public void clearSelectedCheckedImages() {
        selectedPic.clear();
    }

适配器类的getView

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    layout = convertView;
    ViewHolder holder;
    if (convertView == null) {
        holder = new ViewHolder();
        convertView = inflater.inflate(R.layout.add_post_grid_item_layout, parent, false);
        holder.image = (ImageView) convertView.findViewById(R.id.image);
        holder.text = (TextView) convertView.findViewById(R.id.text);
        holder.checkbox = (CheckBox) convertView.findViewById(R.id.itemCheckBox);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    Bitmap myBitmap = BitmapFactory.decodeResource(mConext.getResources(), data.get(position));
    holder.image.setImageBitmap(myBitmap);
    holder.text.setText("" + position);
    holder.checkbox.setTag(position);
    holder.checkbox.setChecked(selectedPic.contains(data.get(position)));
    holder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            int pos = (Integer) buttonView.getTag();
            if (!buttonView.isChecked()) {
                selectedPic.remove(data.get(pos));
            } else if (buttonView.isChecked()) {
                if (!selectedPic.contains(data.get(pos))) {
                    selectedPic.add(data.get(pos));
                }
            }
        }
    });
    return convertView;
}

删除活动中的按钮事件

@Override
    public void onClick(View v) {
        for (Integer obj : adapter.getSelectedChecckedImages()) {
            data.remove(obj);
        }
        adapter.clearSelectedCheckedImages();
        adapter.notifyDataSetChanged();
    }

如果您仍有任何问题我会帮助我