0

I am using a RecyclerView to show some attributes from my class. One of the attributes in that class is a price. An EditView where you can enter a number for your money. A TextView which shows a summed up price from multiselect below.

I have enabled multiselect successfully and summed up the price from each item using a tracker to show in the TextView. I also used my own SelectionPredicate class to prohibit selecting another item if the remaining money is not enough to select an item with a higher price.

Now I also want to grey out all items which you can no longer click because of the remaining money < price.

I found this RecyclerView: Changing background color of a Textview on each List Item which basically is what I need and I successfully got that working with one problem:

I used the logic provided in the above link to do a setBackgroundRessource to color the to expensive items which works, but only on items not in sight. So if I scroll down my RecyclerView and new items come into view which are to expensive, that have the new background and if I scroll back to my starting point of view the items to expensive there are now also coloured differently.

What I am missing is how to trigger the changed background also for the rows which are currently on the screen and to expensive.

I tried putting notifyDataSetChanged() into the tracker.addObserver onSelectionChange method, but that triggers an endless loop until the app crashes as the next background change triggers that again I guess.

So I am looking for a way to do the recolouring once after each select or deselect and not only on the items outside of the screen, but on the onces I can currently see as well.

So in a nutshell, I need the changes apply on the current visible items of my RecyclerView as well, not just the ones out of sight.

Tupsi
  • 11
  • 3

1 Answers1

0

I found the answer to my problem in this post: Force RecyclerView to redraw its items

turns out it is enough to just set the adapter again with

mRecyclerView.setAdapater(adapter);

which I do now in my onItemStateChanged of my SelectionObserver.

Tupsi
  • 11
  • 3
  • I rejoiced to soon and now had to realise that his also resets the recyclerview to the begining of the ArrayList I use. So if you have selected something further down the list (I start with 51 items in this list) your view gets reset to the beginning of the list. That is not the behaviour I have in mind. So I am still looking for a way to refresh the view. As I tried to describe above, the code itselfs already does what I want outside of the visible area, but NOT directly on the screen. I need a way to update the items I currently see to the new facts. – Tupsi Apr 17 '22 at 13:45