I'm making an android app, it's going to be a card game.
val players = arrayOf(
Player(1, "00", 0),
Player(2, "00", 0),
Player(3, "00", 0),
Player(4, "00", 0),
Player(5, "00", 0),
Player(6, "00", 0),
Player(7, "00", 0),
Player(8, "00", 0),
Player(9, "00", 0),
Player(10, "00", 0),
)
val playersState = remember { mutableStateListOf(*players) }
Then I call the display functions:
showPlayers(stateBottomToggle, playersState)
showHandSelection(stateBottomToggle, playersState, value1, value2, value3)
How to create such a list, so that when a change is made in it, the screen is redrawn, without creating the whole list again (as it is resource-intensive).
Ideally, it seems to me, to create a class that will store all common and individual settings, that is, a few switches and this list of players. And pass this instance of the class to each function and work with it. Is it possible to do this and if not, then what can be done?
This is how it works for me at the moment:
players[selectedPlayer-1].hand = key_to_hand_h[hand]
playersState.removeRange(0, 10)
playersState.addAll(players)
That is, I change the original array and then delete the state array and completely recreate it. Of course I find this ineffective and would like to know what can be done?
Have a look at the
setfunction ofMutableListwhich is the type that is returned bymutableStateListOf:To update a single element in a list, you can use the following code:
You can write it even simpler using operator overloading:
By using the
copyfunction, you create a newPlayerinstance with the same values and only update thehandvalue.Please note that
Playershould be a data class with immutable properties like this: