I had the following holder that worked, i.e. I was able to check/uncheck the checkbox. (The checkbox is part of the RecyclerView card):
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
holder.bindData(numbers.get(position));
//in some cases, it will prevent unwanted situations
holder.checkbox.setOnCheckedChangeListener(null);
//if true, your checkbox will be selected, else unselected
holder.checkbox.setChecked(numbers.get(position).isSelected());
holder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
numbers.get(holder.getAdapterPosition()).setSelected(isChecked);
}
});
}
I wanted to implement click of the item on RecyclerView. So, I took from this solution - please check: https://stackoverflow.com/a/26196831/4013399
However, now, the click of the entire card item works, but the checkbox can not be checked/unchecked. How to solve this?
Please help. Thanks.
Update
I made changes - however, "HERE-1,2 and 3" lines are never entered.
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
Log.e("I AM ", "HERE-0");
holder.bindData(numbers.get(position));
final RelativeLayout rlyItem = holder.rlyItem;
final CheckBox checkbox = holder.checkbox;
rlyItem.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View view) {
mListener.onItemClicked(holder.getLayoutPosition());
Log.e("I AM ", "HERE-1");
}
});
checkbox.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View view) {
if (((CheckBox) view).isChecked()) {
checkbox.setChecked(false);
Log.e("I AM ", "HERE-2");
} else {
checkbox.setChecked(true);
Log.e("I AM ", "HERE-3");
}
// Inform to Activity or the Fragment where the RecyclerView reside.
mListener.onItemCheckBoxChecked(((CheckBox) view).isChecked(), holder.getLayoutPosition());
}
});
//in some cases, it will prevent unwanted situations
holder.checkbox.setOnCheckedChangeListener(null);
//if true, your checkbox will be selected, else unselected
holder.checkbox.setChecked(numbers.get(position).isSelected());
checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
numbers.get(holder.getAdapterPosition()).setSelected(isChecked);
Log.e("I AM ", "HERE-4");
//Log.e(Integer.toString(holder.getAdapterPosition()), " IS CHECKED");
}
});
}
First You add the below property in your checkbox
Below is my itemclick
Add below method in your adapter :