carbaj0
09/21/2021, 4:28 PMcarbaj0
09/21/2021, 4:28 PMBradleycorn
09/21/2021, 6:58 PMrememberLazyListState
, it’s just a one liner that calls rememberSaveable() and creates an instance of LazyListState. You could probably create your own implementation that also has a key that would trigger invalidation of the remember
… You could update the key when the Pager changes … something like:
val myLazyListState = rememberSaveable(
saver = LazyListState.Saver,
key=pager.currentPage) {
LazyListState( ... )
}
carbaj0
09/22/2021, 6:26 AMcarbaj0
09/22/2021, 6:28 AMBradleycorn
09/22/2021, 12:06 PMrememberXXX()
methods at all is just to provide defaults for components that need them. In this case, they provide a LazyColumn
composable, and one of the things it needs is a list state:
@Composable
fun LazyColumn(
...
listState: LazyListState
...
) {
For common/basic use cases, they don’t want the developer to have to manually keep track of and provide a list state EVERY time they use it. So, they make the listState property optional and provide a default value. But the default value needs to be remembered, so that’s a lot of code to put into the LazyColumn function definition. So, the final solution is to extract that default value out into a short one liner function they can call:
fun rememberLazyListState(index, offset) = rememberSavable() { .... }
@Composable
fun LazyColumn(
...
listState: LazyListState = rememberLazyListSate()
...
) {
That’s also convenient for the use case where a developer wants to hoist the state, but doesn’t need any special functionality (so, make the remember function public instead of internal). In the end, the purpose of all of the rememberxxx()
methods isn’t necessarily to provide the developer with a one line function for building a remembered state of XXX that is flexible and can handle many situations … the purpose is to provide a simple default, and if the developer wants/needs to hoist the state to a higher level, they can write their own one liner.
All of the above is just IMO.Desmond Teo
09/22/2021, 2:40 PMval state = key(page) {
rememberLazyListState()
}
which will create a new state whenever the key changecarbaj0
09/23/2021, 3:17 AMcarbaj0
09/23/2021, 3:18 AMKey
before, but it seems very useful