Good day, I have an implementation which selects multiple items in which RecyclerView
I have taken the answer of the following question to guide me:
https://stackoverflow.com/a/40551984/14856176
But apparently it does not suit my needs as I face the following problem when selecting the item it works correctly as well as when de-selecting it the problem lies when I select an item and then select a second item right at this moment it does not select it if not that I must give the item a second time to select it and the same thing happens if I select the first (it selects it) then I select the second (it does not select it) and then it selects the third (if it selects it) it is as if every time I select an item when selecting the second the condition is not met, however in logcat
it it is shown that it has been selected but the item does not appear with the selected color, well without further ado I will show what I have for now:
First I create a boolean in the model:
boolean isSelect = false;
And its getter and setter method
public boolean isSelect() {
return isSelect;
}
public void setSelect(boolean select) {
isSelect = select;
}
Then in the adapter inside onBindViewHolder
I create an instance of the model
Complementos user = listaUsuarios.get(position);
Under that instance I create the default color that the items will have (or something like that?)
holder.itemView.setBackgroundTintList(ColorStateList.valueOf(user.isSelect() ? Color.CYAN : Color.WHITE));
Finally, inside the onClickListener
change the color each time it is selected (Validating if it is true or false since the input isSelect is false):
user.setSelect(!user.isSelect());
holder.itemView.setBackgroundTintList(ColorStateList.valueOf(user.isSelect() ? Color.CYAN : Color.WHITE));
However, in the logCat if it appears that the item was pressed:
Press the first item (It was marked)
ViewPostIme pointer 0
ViewPostIme pointer 1
Press the second item (It was not marked but it did appear in the logcat)
ViewPostIme pointer 0
ViewPostIme pointer 1
Press the third item (I know if I mark)
ViewPostIme pointer 0
ViewPostIme pointer 1
And so on...
And I think that would be all as you can see, it is not even something complex, however it does not seem to work for me in the same way, I do not know if it is because I use CardView
? or I have changed setBackGroudColor
to setBackGroundTintList
I hope you can help me in advance thank you very much!
You have to take into account something important with this holder methodology
RecyclerViews
and that is that you use a single instance to create all the items, so you must create variables that manage the differences between each itemin your case you use the class
modelo
so if you create an instance of this class for each item this should work for you without problems, it should be like this:
MainActivity.java
the only thing that changed from the answer you showed was that the onbindviewholder that I change the color of is al
textView
and not alview
it's done like that
What you do is correct, you create a property and its respective getter and setter to define when the element is selected or not.
The problem is that you are modifying the object but you are not modifying the object in the List, which is where the property is obtained to change its color and show it selected.
What you need to do is modify the property directly on the List object either
listaUsuarios.get(i).setSelect(false)
orlistaUsuarios.get(i).setSelect(true)
: