Answer the question
In order to leave comments, you need to log in
Is it ok to store the selected state in a RecyclerView item instance?
Task: change the background of the selected element on click. And set the default background for the element that was selected before (if any).
I decided to store the selected flag in the class that represents the object. When clicked, go through all the elements and set the flag to false for all, and set the value of the selected element to true.
To what extent is this acceptable in terms of the adopted agreements? ( How much WTF? per minute can be heard by showing this code to other developers? )
And one more thing: where, besides Android Studio, can you find actual examples of solving such template tasks so as not to produce banal questions on the Toaster?
Answer the question
In order to leave comments, you need to log in
"I decided to store the selected flag in the class that represents the object" - what kind of object: data or ViewHolder.
If data - it's acceptable, if ViewHolder - no (it's a way to bugs)
It's in vain. Just create a selected variable and store the number in the sheet there, or at least a link to the object itself.
This is just how it's done. We add the isSeleseted field to the item that we pass to the ViewHolder, on click we send an event to the external environment (Activity / Presenter / Fragment etc) which element is selected (we process our list) and update the recycler, preferably with DiffUtils.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question