Challenge - 5 Problems
LazyGrid Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ ui_behavior
intermediateWhat is the visible item count in this LazyVerticalGrid?
Consider a LazyVerticalGrid with 3 columns and 10 items. Each item is a fixed size box. How many items will be visible on screen if the grid height fits exactly 4 rows?
Android Kotlin
LazyVerticalGrid(columns = GridCells.Fixed(3)) { items(10) { index -> Box(Modifier.size(100.dp)) { Text("Item $index") } } }
Attempts:
2 left
๐ก Hint
Think about how many items fit in each row and how many rows fit in the visible height.
โ Incorrect
With 3 columns and 4 rows visible, up to 3 * 4 = 12 items can be shown. Since there are only 10 items, 10 items will be visible.
โ lifecycle
intermediateWhen does LazyVerticalGrid compose items?
In Jetpack Compose, when does LazyVerticalGrid compose its child items?
Attempts:
2 left
๐ก Hint
Lazy means it does not create all items immediately.
โ Incorrect
LazyVerticalGrid composes only visible items plus some buffer items to optimize performance and memory.
๐ Syntax
advancedWhich code snippet correctly creates a LazyHorizontalGrid with 2 rows?
Select the Kotlin code that correctly creates a LazyHorizontalGrid with exactly 2 rows.
Attempts:
2 left
๐ก Hint
Check the parameter name for rows in LazyHorizontalGrid.
โ Incorrect
LazyHorizontalGrid requires the 'rows' parameter with GridCells.Fixed or GridCells.Adaptive. Option A uses correct syntax.
advanced
How to preserve scroll position in LazyVerticalGrid after navigation?
You navigate away from a screen with a LazyVerticalGrid and then return. How do you preserve the scroll position of the grid?
Attempts:
2 left
๐ก Hint
There is a special state class for LazyGrid scroll state.
โ Incorrect
rememberLazyGridState() creates a state object that tracks scroll position and can be reused to restore it.
๐ง Debug
expertWhy does this LazyVerticalGrid crash with IllegalArgumentException?
Given this code, why does the app crash with IllegalArgumentException: 'GridCells.Fixed must be greater than 0'?
Android Kotlin
LazyVerticalGrid(columns = GridCells.Fixed(0)) { items(5) { Text("Item $it") } }
Attempts:
2 left
๐ก Hint
Check the parameter passed to GridCells.Fixed carefully.
โ Incorrect
GridCells.Fixed(0) is invalid because the number of columns must be positive. This causes the IllegalArgumentException.
