I am trying to make a horizontal list in which the elements on click change color.
Faced with the fact that when the color of the first element changes, the 7th, 13th, 19th, etc., changes color.
In principle, a large list is not needed, 10-15 elements would suffice.
Or somehow tie the elements to the position?
public class ContactAdapter extends RecyclerView.Adapter<ContactAdapter.ContactViewHolder> { private List<ContactInfo> contactList; boolean button = false; public ContactAdapter(List<ContactInfo> contactList) { this.contactList = contactList; } @Override public int getItemCount() { return contactList.size(); } @Override public void onBindViewHolder(ContactViewHolder contactViewHolder, int i) { ContactInfo ci = contactList.get(i); contactViewHolder.vName.setText(ci.name); contactViewHolder.vSurname.setText(ci.surname); //contactViewHolder.butto.setText(ci.name); //contactViewHolder.vEmail.setText(ci.email); //contactViewHolder.vTitle.setText(ci.name); } @Override public ContactViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) { View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.card_layout, viewGroup, false); return new ContactViewHolder(itemView); } public class ContactViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener { protected TextView vName; protected TextView vSurname; protected Button butto; protected TextView vEmail; protected TextView vTitle; public ContactViewHolder(View v) { super(v); vName = (TextView) v.findViewById(R.id.time); vSurname = (TextView) v.findViewById(R.id.days); butto = (Button) v.findViewById(R.id.but); //vEmail = (TextView) v.findViewById(R.id.txtEmail); // vTitle = (TextView) v.findViewById(R.id.title); butto.setOnClickListener(this); butto.setOnLongClickListener(this); v.setOnClickListener(this); v.setOnLongClickListener(this); } @Override public void onClick(View v) { if (v.equals(butto)) { if (!button) { butto.setBackgroundResource(R.drawable.active_y); } else { butto.setBackgroundResource(R.drawable.inactive_y); } button = !button; } } @Override public boolean onLongClick(View v) { if (v.equals(butto)) { removeAt(getPosition()); } return false; } public void removeAt(int position) { contactList.remove(position); notifyItemRemoved(position); notifyItemRangeChanged(getPosition(), contactList.size()); } }}