Home > Software design >  Is there a way to get scrollstate from Lazyrow
Is there a way to get scrollstate from Lazyrow

Time:05-02

I have made a LazyRow that i now want to be able to get the scrollposition from. What i understand Scrollablerow has been deprecated. (correct me if im wrong) The thing is that i cant make a scrollablerow so i thought lets make a lazy one then. but i have no clue how to get scrollposition from the lazyrow. i know how to get index but not position if that eaven exists. here is what i have tried.

val scrollState = rememberScrollState()
LazyRow(scrollState = scrollstate){

}

CodePudding user response:

For LazyScrollers, there are separate LazyStates.

I think there's just one, in fact, i.e. rememberLazyListState()

Pass that as the scroll state to the row and then you can access all kinds of info. For example, you could get the index of the first visible item, as well as its offset. There are direct properties for this stuff in the object returned by the above initialisation. You can also perform some more complex operations using the lazyListState.layoutInfo property that you get.

Also, ScrollableRow may be deprecated as a @Composable, but it is just refactored, a bit. Now, you can use the horozontalScroll() and verticalScroll() Modifiers, both of which accept a scrollState parameter, which expects the same object as the one you've created in the question.

Usually, you'd use LazyScrollers since they're not tough to implement and also are super-performant, but the general idea is that they are used with large datasets whereas non-lazy scrollers are okay for small sized lists and stuff. This is because the lazy ones cache only a small fraction of the entire list, making your UI peformant, which is not something regular scrollers do, and not a problem for small datasets.

They're like equivalents of RecyclerView from the View System

  • Related