I want to make all the invisible checkbox visible by longclick of image. But this makes only one checkbox visible.
Making edits as per the suggestions
In gridadapter class..
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;
}
I previously did using object of adapter in 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);
}
Now i want everything to happen in adapter class itself.
Thanks in advance!
The ViewHolder pattern you are using in the Adapter requires that item state live in the data itself; operating directly on the views themselves (as in posted examples) will result in weird state issues when scrolling. The ViewHolder.onBind() method (or custom equivalent) needs to be able to correctly set whether the checkbox is checked or not based on the data element, so getData() should be transformed such that each item has a boolean isChecked field.
Alternately, if all items will always be either checked or unchecked together, you can probably have a single boolean field in the Adapter that can be used to check the box onBind() if needed. A method in the adapter can change the boolean then notifyDataSetChanged() to re-bind everything.
More detail could be provided if we could see more of the GridViewAdapter implementation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With