Good day, I have problems when trying to select only one element of the RecyclerView
previous one I had implemented a logic that selects several elements for me but this time I need only one to be selected from the list for example if I have 3 items called apple, pear and strawberry yes I select apple and then I select pear I want apple to de-select and so on as I select different items
This is the code I have to select several:
if(listaUsuarios.get(position).isSelect() == true){
listaUsuarios.get(position).setSelect(false);
arrayList.remove(ObtenerComplementos.nombreOpcion);
holder.view.setBackgroundTintList(ColorStateList.valueOf(Color.WHITE));
}
else{
listaUsuarios.get(position).setSelect(true);
arrayList.add(ObtenerComplementos.nombreOpcion);
holder.view.setBackgroundTintList(ColorStateList.valueOf(Color.rgb(244,167,29)));
}
"isSelect" and "setSelect" are variables that I bring from my model:
public boolean isSelect() {
return isSelect;
}
public void setSelect(boolean select) {
isSelect = select;
}
Is there a way to adapt this code and make it only select 1 item instead of several, I hope you can help me, thank you very much in advance!
If you want to select only one object you can update the list so that it eliminates the selection of all the elements except the selected one, for this you can use this method, which would receive the list and the position of the element:
Clicking the element calls the method like this:
in this way you will get an updated list with only the element of the position.