gpt4 book ai didi

java - 更改 gridview 适配器类中所有复选框的可见性

转载 作者:行者123 更新时间:2023-12-02 01:58:15 24 4
gpt4 key购买 nike

我想通过长按图像使所有不可见的复选框可见。但这只会使一个复选框可见。

   public GridViewAdapter(Context context, int layoutResourceId, 
ArrayList<ImageItem> data) {

super(context, layoutResourceId, data);

this.layoutResourceId = layoutResourceId;//id of grid_item_layout
this.context = context;
this.imageFiles = imageFiles;
this.data = data;
}
public View getView(final int position, final View convertView, @NonNull
ViewGroup parent) {
row = convertView;
final ViewHolder holder;

if (row == null) {

LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ViewHolder();
//find resource...
holder.image = (ImageView) row.findViewById(R.id.image);
holder.imageTitle = (TextView) row.findViewById(R.id.text);
holder.check = (CheckBox) row.findViewById(R.id.checkbox);
row.setTag(holder);
}
else
{ holder = (ViewHolder) row.getTag();}
holder.check.setOnCheckedChangeListener(null);
holder.check.setFocusable(false);

holder.image.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
holder.check.setVisibility(View.VISIBLE);
return true;
}
});
holder.check.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

if (data.get(position).isSelected()) {
data.get(position).setSelected(false);
} else {
data.get(position).setSelected(true);
}

}
});


holder.check.setChecked(data.get(position).isSelected());
holder.imageTitle.setText(data.get(position).getTitle());

holder.image.setImageBitmap(data.get(position).getImage());
return row;
}
static class ViewHolder {
TextView imageTitle;
ImageView image;
CheckBox check;
}

我的 ImageItem 类

public class ImageItem {
private Bitmap image;
private boolean selected;
private String title;

public ImageItem(Bitmap image, String title) {
super();
this.image = image;
this.title = title;
}

public Bitmap getImage() {
return image;
}

public void setImage(Bitmap image) {
this.image = image;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}


public void setSelected(boolean selected) {
this.selected = selected;
}
public boolean isSelected() {
return selected;
}

}

这用于维护复选框的状态。我之前更改了 mainactivity 中的可见性,如下所示

GridAdapter = new GridViewAdapter(this, R.layout.grid_item_layout, 

getData());
gridView.setAdapter(gridAdapter);
// gridView.OnLongclick....
for(int index=0;index< adapterView.getChildCount();index++) {
View nextchild = (adapterView.getChildAt(index));
CheckBox checkBox = (CheckBox) nextchild.findViewById(R.id.checkbox);
checkBox.setVisibility(View.VISIBLE);
}

但是我在这个方法中遇到了一些其他问题,所以想更改适配器类本身中复选框的状态。我为这个问题思考了两周。任何帮助表示赞赏。

最佳答案

根据适配器中的此语句:

holder.check.setChecked(data.get(position).isSelected())

看起来像ImageItem已在 selected 中维护复选框的状态成员(member)字段。如果是这样,那么您可以迭代 ImageData 的列表,而不是迭代列表中的实际 View 并切换每个 View 的复选框可见性。并稍后通知适配器数据已更改。

类似这样的事情:

List<ImageItem> list = // ...

// Mark everything as selected
for(ImageItem item : list){
item.setSelected(true);
}

// Refresh the list
gridAdapter.notifyDataSetChanged();

这个想法是更新模型本身,然后触发列表的更新。

已更新
好的,根据您的评论,让我尝试再次解释一下:

所以目前您有这段代码可以打开所有复选框:

GridAdapter = new GridViewAdapter(this, R.layout.grid_item_layout, getData());
gridView.setAdapter(gridAdapter);

// gridView.OnLongclick....
for(int index=0;index< adapterView.getChildCount();index++) {
// ....
}

我的建议如下:

// Get the data and store it in a local copy
List<ImageItem> items = getData();

// Mark each item in the list as selected
for(ItemItem item : items){
item.setSelected(true);
}

// Pass the updated list to the adapter
GridAdapter = new GridViewAdapter(this, R.layout.grid_item_layout, items);
gridView.setAdapter(gridAdapter);

// Assuming that your adapter is properly implemented,
// all checkboxes should be marked as selected now.

关于java - 更改 gridview 适配器类中所有复选框的可见性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52018782/

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