Challenge - 5 Problems
Pagination Pro
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2:00remaining
What happens when you scroll to the bottom of this RecyclerView?
Given this Kotlin code snippet for a RecyclerView with pagination, what is the expected behavior when the user scrolls to the bottom?
Android Kotlin
recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrolled(rv: RecyclerView, dx: Int, dy: Int) {
super.onScrolled(rv, dx, dy)
val layoutManager = rv.layoutManager as LinearLayoutManager
val totalItemCount = layoutManager.itemCount
val lastVisible = layoutManager.findLastVisibleItemPosition()
if (lastVisible == totalItemCount - 1) {
loadNextPage()
}
}
})Attempts:
2 left
💡 Hint
Think about what happens when the last visible item matches the total items minus one.
✗ Incorrect
The code listens for scroll events and checks if the last visible item is the last in the list. If yes, it triggers loading the next page.
🧠 Conceptual
intermediate1:30remaining
Why use pagination in mobile apps?
Which of these is the main reason to implement pagination in a mobile app?
Attempts:
2 left
💡 Hint
Think about how loading large data sets affects app speed and memory.
✗ Incorrect
Pagination helps by loading only a small part of data at a time, saving memory and making the app faster.
📝 Syntax
advanced1:00remaining
What is the output of this Kotlin code for a paginated list?
Consider this code snippet that appends new items to a list during pagination. What will be the size of 'items' after running?
Android Kotlin
val items = mutableListOf(1, 2, 3) val newItems = listOf(4, 5) items.addAll(newItems) println(items.size)
Attempts:
2 left
💡 Hint
What does addAll() do to the list?
✗ Incorrect
addAll() adds all elements from newItems to items, increasing its size from 3 to 5.
❓ lifecycle
advanced1:30remaining
When should you reset pagination state in an Android app?
In an app with pagination, when is it best to reset the current page number and clear loaded data?
Attempts:
2 left
💡 Hint
Think about when the data source changes completely.
✗ Incorrect
Resetting pagination on new search or refresh ensures the list shows fresh data from page one.
🔧 Debug
expert2:30remaining
Why does this pagination code cause duplicate items?
This Kotlin code appends new page data but sometimes shows duplicates. Why?
Android Kotlin
fun loadNextPage() {
val newData = fetchData(page)
items.addAll(newData)
adapter.notifyDataSetChanged()
page += 1
}
// fetchData returns the same data for page 1 and 2 by mistakeAttempts:
2 left
💡 Hint
Check what fetchData returns for each page.
✗ Incorrect
If fetchData returns the same data for multiple pages, adding it causes duplicates in the list.