'Where to update visible RecyclerView Items?

I have a recyclerview showing items like this: MyRecyclerView

Selecting items calculates a total price from the number you see in the circle of each item, adding them up while you select more items. The upper right corner shows how mach money is left.

Now I want to grey out items if the money left is smaller of the price for an item, which can be seen in the first item "Enlightenment". I have this achieved by setting the alpha inside the OnBindViewHolder to 0.5F instead of 1.0f.

My problem is, that this works only for items currently not seen, aka outside of the screen. In the example of my screenshot I scrolled down first until Enlightenment was out of sight and then scrolled it back in to view achieving what you can see. But I want to visible items to also reflect if they are buyable or not the moment I select another item. For instance in the screenshot, everything which is currently shown but the two already selected items should be greyed out, as there is only 20 left which is not enough the items you see.

How can I achieve this for the items visible?



Solution 1:[1]

Hej Tupsi,

could you provide the code of your Adapter? Otherwise, I find it hard to give you an advice to improve your current code.

I think the problem is that you do not update the view of a single ViewHolder correctly. Maybe a notifyItemChanged call is missing to trigger the update of an ViewHolder.

Also to trigger updates on the other ViewHolders, the adapter must hold the current total value. Each ViewHolder must report to the adapter if it is clicked, the adapter then updates it state and triggers an update on all ViewHolder providing the new total value.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Daniel Knauf