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()); } }} 

2 answers 2

RecyclerView has a helper class called Recycler for managing reuse of items.
This class includes the setViewCacheSize() method, which allows you to specify how many items should be saved before reuse, but this is still not a complete solution to your problem.

From my point of view, the most appropriate solution would be to add a service array for storing states (for your case, there will be enough boolean values, but in general, an array of integer values ​​if there are several colors) with a size similar to the size of the list of models. This array will store marks about which color to use for a particular item.

The element number in the array will correspond to the item number. By clicking, besides changing the color, in the case of Boolean values, you also need to invert the states of the element in the array (or write the value if there are several colors).
In the onBindViewHolder method, onBindViewHolder need to check which state is set for the current element and assign the appropriate color (mandatory for both states, otherwise there will be overlays when reused).

In addition, although the ViewHolder class has methods for getting the current position in the adapter ( getAdapterPosition() and getLayoutPosition() ) to simplify the code, I would transfer the listener and the processing of clicks directly to the adapter.

    For good you need to save information and the color of your model. For example, you pass contactList of the ContactInfo type - make it so that ContactInfo can store color information when it is clicked, and display it accordingly.