Good day. I have a RecyclerView that lists the CardView. Each CardView consists of TextView and CheckBox. Mark CheckBox by clicking on CheckBox itself is not very convenient (because the checkbox area is small for a finger). I would like to checkmark by clicking around the whole CardView. Put RecyclerView on ClickListner:

rv.addOnItemTouchListener(new RecyclerItemClickListener(getActivity(), rv, new RecyclerItemClickListener.OnItemClickListener() { @Override public void onItemClick(View view, int position) { Toast.makeText(getActivity(), "Click" + AddFilterAdapter.getBox(), Toast.LENGTH_SHORT).show(); } })); 

Further, as it seems to me, you need to determine the CardView position in the adapter and check the required CheckBox by position. But how to implement it? Tell me an example.

    1 answer 1

    You can add checkbox logic directly to the view holder, for example

     public class ViewHolder extends RecyclerView.ViewHolder{ private View itemView ; private CheckBox checkbox; public ViewHolder(View itemView){ super(itemView) this.itemView=itemView; this.checkbox =(CheckBox) itemView.findViewById(R.id.checkbox) } void bindItem(RecyclerView item, On click listener onClick) { CheckBox.isChecked(item.isChecked): itemView.setOnClickListener(onClick) ; } } public class RecyleclerItem { private <YOUR ITEM TYPE> object ; private boolean isChecked ; } onBindViewHolder(holder, position) { RecyclerView item = itemList.get(position); holder.bindItem(item, v-> { item.isChecked =! item.ischecked; notifyItemChenged(position); } } 
    • The answer is poorly formatted, since I dialed it from the phone, but I hope the idea itself was able to get across - Sergey Zabelnikov
    • Could you explain what v-> means? - ivanovd422
    • @ Denis422 is a lambda expression - artemiygreg