Hi everyone! What is the best way to check if I st...
# compose
d
Hi everyone! What is the best way to check if I still can scroll in LazyList? something that replace the computeVerticalScrollOffset of the recycler view?
d
What are you trying to achieve?
I do not know if this is the “best” way but you could check to see if all the items in the list are not visible.
LazyListState#layoutItemInfo
has this infomation.
j
if you don’t need to deliver this functionality immediately, you could wait for an upcoming release of Compose, because last week a change was merged which adds
canScrollForward
and
canScrollBackward
properties to ScrollableState.. and it sounds like that’s exactly what you need if you do need to deliver this functionality immediately then just grab the current
value
and the
maxValue
of your scroll state and compare them (if you’re checking for end of list)
d
@dewildte @james I want to achieve the following behavior: By click on the phone back button, I scroll to top if I can, and if not, I dismiss the app
j
@Daniel Okanin then you probably want to create your own derived state which checks if there’s room to scroll backward (ie. the scroll state value is >0) so something like:
Copy code
val scrollState = rememberScrollState()
val canScrollBackward by remember {
    derivedStateOf {
        scrollState.value > 0
    }
}
then in your back handler code you can simply check
if (canScrollBackward) { your scroll-to-top code } else { your dismiss code }
once you get that working you might even want to take it a little further and introduce some threshold, because if the user has scrolled a tiny bit (eg. only a few pixels) then they’re effectively still at the “top” already, but
canScrollBackward
will be true because the scroll state is >0, and so the behaviour might feel a bit strange to the user.. but I’ll leave that one up to you 😄