https://kotlinlang.org logo
#compose
Title
# compose
c

Chris Johnson

02/09/2022, 11:04 PM
I have a question about LazyColumn/Row. Does it still recycle/remember composables that scroll off screen? Or does it just create a new composable everytime a new one comes into view? I assume there has to be some sort of caching going on since you can put
remember { X }
inside LazyColumn/Row items and the next time you scroll to that item it remembers that state. Or maybe I've gotten lucky in that and there's a cache amount that I haven't scrolled past yet.
a

Andrey Kulikov

02/09/2022, 11:35 PM
yeah, only last two previously-visible items are currently left active for reuse so you were happy to see this value to survive scrolling off the viewport. there is a high chance this behavior can change in future so I would not rely on that
👍 1
👍🏼 1
c

Chris Johnson

02/09/2022, 11:42 PM
Thanks for the answer! So if your lazy list item composable has something that's relying on a remembered state, it's better to rethink that and put it somewhere else( potentially in the item's data state) and rely on recomposition of the list ?
a

Andrey Kulikov

02/09/2022, 11:47 PM
if you want your data to live longer that the item is visible you or hoist this data up - for example somewhere on the same level where you define your LazyColumn, or use rememberSaveable in the item composable
c

Chris Johnson

02/10/2022, 12:00 AM
Gotcha. That makes sense. Is there a way to configure how many views LazyColumn will cache? I'm running into an issue where I need the a pixel position of every item and to check if that's currently on screen. The way I'm currently doing this is
onGloballyPositioned
and comparing layoutCoordinates. This doesn't work though when you're scrolling up past that 1-2 view cache because it's no longer tracking that view's position. Would wrapping every item in
ReusableContent
make it so they're all cached?
a

Andrey Kulikov

02/10/2022, 12:10 AM
please use LazyListState.layoutInfo to know what items are currently visible and their offsets. note that in order to make it performant you need to observe them not during the composition, but via snapshotFlow, similarly to how it is described in https://developer.android.com/jetpack/compose/lists#react-to-scroll-position
c

Chris Johnson

02/10/2022, 12:13 AM
Ah, offset is what I was missing. I had a snapshowFlow setup to listen for visibleItem changes and then fire/reset events based on what was visible, but now we're having to deal with nested things inside of items in the list. I think offset could help with that. Ty!
72 Views