S
S
Sashulya2019-12-13 08:52:47
Android
Sashulya, 2019-12-13 08:52:47

How to avoid duplicate clicks in RecyclerVuew?

Good day to all! Tell me, please, what could be the problem and how to solve it.
RecyclerView in the adapter in the implementation of the viewHolder, I hang on holder.itemView onClick. The task of the click is to make the detailed layout visible when clicking on the RV element. And everything is fine, everything works, but if you scroll through the list, there will still be open elements, and when you go back, you may find that the element that you selected is not open.
Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Zagaevsky, 2019-12-13
@Shikato

RecyclerView reuses views and viewholders. Therefore, in each onBindViewHolder, you should expect that a holder will come that has already been used before, and, accordingly, the view has also been used.
The solution is this - when you click, you need to change the data set (remember that the element is open) and call notifyItemUpdated. Expand only in onBindViewHolder.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question