Home > Software design >  Android Compose: Difference between LazyColumn and Column with verticalScroll
Android Compose: Difference between LazyColumn and Column with verticalScroll

Time:09-16

I tried to look for reasons to use LazyColumn vs Column with verticalScroll. What is the difference between them? Like why would one be an ideal choice compare to other. They both make screen scrollable, isn't it? Would choosing one over the other one be wrong in any case?

CodePudding user response:

LazyColumn

A LazyColumn is a vertically scrolling list that only composes and lays out the currently visible items. It’s similar to a Recyclerview in the classic Android View system.

Column

A Column will show each child below the previous children. It’s similar to a LinearLayout with vertical orientation.

Imagine that you want to display a large amount of data with an unknown number of items. If you decide to use a Column/Row layout, this could translate into a lot of performance issues because all the items will compose whether they’re visible or not. The Lazy option lets you lay out the components when they’re visible. Resulting in much better performance when dealing with larger amount of elements in list/grid

CodePudding user response:

LazyColumn is RecyclerView counterpart of Compose while Column with verticalScroll is ScrollView counterpart.

Any Composable inside Column with vertical scroll enters composition the moment Column is composed while LazyColumn uses SubcomposeLayout to subcompose Composables on screen and one extra when you reach last visible item on Screen.

Also LazyColumn offers some features where Column doesn't

flingBehavior: FlingBehavior = ScrollableDefaults.flingBehavior()

let's so you easily implement flingBehavior especially with 1.3.0-beta02 snap behavior can implemented with rememberSnapFlingBehavior()

Also rememberLazyListState() provides information about first item index, offset and layoutInfo and visible items which enables more customization than rememberScrollState() like this color and scale animation using visible item positions.

  • Related