Scrollable columns and rows are great when you have static content, like in the previous examples.
However, they aren't a good idea for data collections that are dynamic.
That's because scrollable composables compose and render all the elements inside eagerly, which can
be a heavy operation when you have a large number of elements to display.
In such cases, as you know from the traditional view system, you'd use a recycler view to optimize
the loading and rendering of the visible elements on the screen.
But how does Jetpack Compose deal with this issue?
Let's find out in this video.
To display a large collection of elements in Android, you use the recycler view.
The only elements Recycler View renders are the ones visible on the screen only after the user begins
to scroll.
Does it render the new elements and display them on the screen?
It then recycles the elements that go off the screen into a pool of view holders.
When you scroll back to see the previous elements, it renders them from the pool.
Thanks to this behavior, Rerendering is so quick that it's almost as if the elements were never removed
from the screen in the first place.
This optimization mechanisms gives recycler view its name.
Loading data only when it's needed is called lazy loading, and Jetpack Compose doubles down on this
method to handle lists.
The main two components you use for lazy lists in compose are the lazy Column and Lazy Row.
Recycler view You uses a layout manager to set its own orientation, but Jetpack Compose doesn't have
layout managers.
Instead, you use two different composable functions to change the orientation.
The composables work in almost the same way as the recycler view, but without needing to recycle.
So here I create a new composable.
I'll name it as my lazycolumn.
And inside this composable function I create lazy column, and inside it I'll create items, items,
list, and for every item go and create a text composable.
Like this.
Don't worry, we'll clarify everything.
Create a val items list equals to list of.
And here I will create a list of strings displaying the name of my applications on Play Store.
Start with master coding app.
Here we created a list called items List of Type string and we passed four items.
Then we used the Lazycolumn.
If we go to the declaration of this composable control and left mouse, we can see the Lazycolumn contains
many parameters like the modifier state, content, padding, reverse layout, vertical arrangement,
horizontal alignment, flange behavior, user scroll, and enabled and content.
So the lazycolumn is a composable function that efficiently displays a vertical list of items.
Unlike traditional column, Lazycolumn only composes and lays out visible items, which significantly
improves performance especially for large data sets and to create a lazy column.
It's very simple.
We need to specify the items you want to display using the items or item blocks, so we use the items
items.
This function is part of the Lazy Collins DSL domain specific language items list is the data source,
which is a list of items to be displayed.
This list can be any type of list, such as list of string, list of custom data, and etc. so items
is a function that takes a list and a lambda to render each item.
We can use item if you have specific single item, but here we have many items.
We use the items function inside the trailing lambda.
For each item parameter.
Inside the list of items, you create a new text composable, and this lambda represents the function
to transform each of the objects within the items to a composable element.
Again, guys, I want from you to focus with me inside this trailing function Lambda expression.
This is a lambda function that defines how each item in the list should be rendered.
Item is a parameter representing the current item in the iteration.
The text inside the lambda.
You specify the UI for each item.
Here, each item for the items list is passed to the text composable, which renders the item as a text.
Let's call this function in the Setcontent and run our application.
And here we go.
This is our list.
This is our lazy column.
In the next video, we'll deep dive into lazy columns and make our customized lazy columns.