Below is the my code.
holder.followDiseaseCheckBox.setOnClickListener(new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
The recycler view recycles the view in OnBindViewHolder. So when items are clicked it gets reflected in some other positions. To create a global SparseBooleanArray to store the clicked position.
private final SparseBooleanArray array=new SparseBooleanArray();
Then inside final viewholder add the clickListener and onClick store the position of the clicked item.
public class ViewHolder extends RecyclerView.ViewHolder {
public YOURVIEW view;
public ViewHolder(View v) {
super(v);
view = (YOURVIEW) v.findViewById(R.id.YOURVIEWID);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
array.put(getAdapterPosition(),true);
notifyDataSetChanged();
}
});
}
}
And in inside OnBindViewHolder,
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
if(array.get(position)){
holder.followDiseaseCheckBox.setChecked(true);
}else{
holder.followDiseaseCheckBox.setChecked(false);
}
}