0
0
Android Kotlinmobile~20 mins

LazyGrid in Android Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
LazyGrid Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
1:30remaining
What 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")
    }
  }
}
A12 items
B10 items
C9 items
D15 items
Attempts:
2 left
💡 Hint
Think about how many items fit in each row and how many rows fit in the visible height.
lifecycle
intermediate
1:30remaining
When does LazyVerticalGrid compose items?
In Jetpack Compose, when does LazyVerticalGrid compose its child items?
AOnly the first item initially, then others on demand
BAll items at once when the grid is first displayed
COnly the items currently visible on screen and a few extra for smooth scrolling
DItems are composed randomly regardless of visibility
Attempts:
2 left
💡 Hint
Lazy means it does not create all items immediately.
📝 Syntax
advanced
2:00remaining
Which code snippet correctly creates a LazyHorizontalGrid with 2 rows?
Select the Kotlin code that correctly creates a LazyHorizontalGrid with exactly 2 rows.
A
LazyHorizontalGrid(rows = GridCells.Fixed(2)) {
  items(5) { Text("Item $it") }
}
B
LazyHorizontalGrid(columns = GridCells.Fixed(2)) {
  items(5) { Text("Item $it") }
}
C
LazyHorizontalGrid(rows = 2) {
  items(5) { Text("Item $it") }
}
D
LazyHorizontalGrid(GridCells.Fixed(2)) {
  items(5) { Text("Item $it") }
}
Attempts:
2 left
💡 Hint
Check the parameter name for rows in LazyHorizontalGrid.
navigation
advanced
2:00remaining
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?
AUse rememberLazyGridState() and pass it to LazyVerticalGrid's state parameter
BUse rememberScrollState() and pass it to LazyVerticalGrid's state parameter
CSave the scroll position manually and scroll to it on return
DLazyVerticalGrid automatically preserves scroll position without extra code
Attempts:
2 left
💡 Hint
There is a special state class for LazyGrid scroll state.
🔧 Debug
expert
2:30remaining
Why 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") }
}
ALazyVerticalGrid requires GridCells.Adaptive, not Fixed
BText composable inside items must have a Modifier
CThe items count must be greater than the number of columns
DGridCells.Fixed cannot be zero or negative; it must be at least 1
Attempts:
2 left
💡 Hint
Check the parameter passed to GridCells.Fixed carefully.