我想通过长按图像使所有不可见的复选框可见。但这只会显示一个复选框。
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);
}
但是我在此方法中遇到了其他一些问题,因此想更改适配器类本身中复选框的状态。埃姆对此事进行了两个星期的调查。任何帮助表示赞赏。
答案 0 :(得分:2)
根据适配器中的以下语句:
holder.check.setChecked(data.get(position).isSelected())
看起来ImageItem
已经在selected
成员字段中维护了复选框的状态。如果是这样,那么您可以遍历ImageData
的列表并稍后通知适配器数据已更改,而不是遍历列表中的实际视图并切换每个复选框的可见性。
类似这样的东西:
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.