gpt4 book ai didi

android - 如何从 Android Gridview 中删除复选框

转载 作者:行者123 更新时间:2023-11-30 01:43:18 25 4
gpt4 key购买 nike

我正在实现删除 Android GridView 中带有图像的复选框。我已经删除了选中的图像,但复选框按原样显示。如何也从网格中删除复选框。有人可以帮助我吗解决复选框的这个问题。
这是我在 Adapter 类中的 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>

最佳答案

初始化selectedPic数​​组,存放在Adapter上选中的图片

private ArrayList<Integer> selectedPic = new ArrayList<Integer>();

创建此方法来跟踪适配器类上选中的复选框位置

public ArrayList<Integer> getSelectedChecckedImages() {
return selectedPic;
}

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

获取适配器类的View

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

删除 Activity 中的按钮事件

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

如果你还有什么问题,请告诉我,我会帮助你

关于android - 如何从 Android Gridview 中删除复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34132284/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com