Tom De Decker
05/02/2023, 7:30 AMvar selectedIndex by remember(myList) { prev ->
// prev should be null on the first composition while
// it should be the previous remembered value on recompositions
mutableStateOf(prev?.coerceIn(myList.indices) ?: 0)
}
I currently do the following but this resets the index to 0 each time the list changes:
var selectedIndex by remember(myList) { mutableStateOf(0) }
Does anyone know if this is possible?ephemient
05/02/2023, 8:48 AMvar mutableSelectedIndex by remember { mutableStateOf(0) }
val selectedIndex = remember { derivedStateOf { mutableSelectedIndex.coerceIn(myList.indices) } }
(terrible naming aside, pick something more meaningful for your use case)
read from the latter, write to the former, doesn't that work?Tom De Decker
05/02/2023, 9:16 AMephemient
05/02/2023, 9:30 AMmutIndex = index
would resolve that but it's a bit annoying to have to do that in addition…Zach Klippenstein (he/him) [MOD]
05/02/2023, 4:53 PMZach Klippenstein (he/him) [MOD]
05/02/2023, 4:53 PMZach Klippenstein (he/him) [MOD]
05/02/2023, 4:54 PMZach Klippenstein (he/him) [MOD]
05/02/2023, 4:56 PMTom De Decker
05/03/2023, 10:14 AMTabRow
) where I want to keep track of the currently selected tab. When the screen orientation changes, the amount of tabs can also change due to some information being moved from the main view to an extra tab in portrait mode. What I'd hoped to achieve was to keep track of the selected tab index (which is necessary for the TabRow) without exceeding the bounds of the list of tabs, truncating/coercing the index when applicable.
As you mentioned I could store an ID instead of the index, with the caveat being that when the amount of tabs changes from e.g. 3 to 2, and our selection is on the last tab, we no longer have enough information to move the selected element from the 3rd element to the 2nd element (as we don't know at what index the last selected tab was). In other words, I want to keep the selected tab as close as possible to the previous location should that tab no longer be available.
Any thoughts? I'd love to do this properly but I can't really find a "clean" way to do this.Zach Klippenstein (he/him) [MOD]
05/04/2023, 3:00 PMTom De Decker
05/04/2023, 3:21 PMZach Klippenstein (he/him) [MOD]
05/04/2023, 5:37 PM