androidandroid-studio

Can I pass 10,000 images into a LazyColumn since it loads items lazily?


I'm currently working on a Gallery App. Previously, in XML, I implemented lazy loading separately. Now that I've moved to Jetpack Compose, I’d like to know if I still need to use traditional lazy loading strategies, like Paging, or if it's possible to pass a large number of items (e.g., 10,000) directly into a LazyColumn or LazyVerticalGrid without issues.

Thanks!


Solution

  • You need both, they complement each other.

    Whatever you feed into the LazyColumn, it only builds the UI for the items that are currently displayed, i.e. that fit into the visible area of the LazyColumn. The user can scroll items in and out of the visible area, so whenever the item isn't visible anymore, its UI isn't drawn anymore. The size of the list has no significant impact, 10,000 items won't be an issue.

    That's the main feature of the LazyColumn, it makes large lists efficient to display.

    There is just one problem left: The large list itself. When the items need to be read from the file system or - even worse - over the network, then assembling the list will take a considerable amount of time. Time that the LazyColumn needs to wait before it can display even the first item.

    That's where Paging helps out: It allows you to read the data lazily in chunks, when they are needed. So you do not need to wait for the entire list to be loaded to start displaying the first items.

    Conclusion: You want to use LazyColumn to efficiently display a list of items that is larger than the available screen space. You want to use Paging when you want to start displaying data before it is fully loaded.