0
0
Android Kotlinmobile~10 mins

Sticky headers in Android Kotlin - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to set a sticky header in a RecyclerView using a LayoutManager.

Android Kotlin
recyclerView.layoutManager = [1]
Drag options to blanks, or click blank then click option'
AStaggeredGridLayoutManager(2, LinearLayoutManager.VERTICAL)
BLinearLayoutManager(this)
CGridLayoutManager(this, 2)
DFlexboxLayoutManager(this)
Attempts:
3 left
💡 Hint
Common Mistakes
Using GridLayoutManager when a simple vertical list is needed.
Forgetting to assign a LayoutManager to the RecyclerView.
2fill in blank
medium

Complete the code to create a sticky header by attaching an ItemDecoration to the RecyclerView.

Android Kotlin
val stickyHeaderDecoration = [1](adapter)
recyclerView.addItemDecoration(stickyHeaderDecoration)
Drag options to blanks, or click blank then click option'
ASnapHelper
BDividerItemDecoration
CItemTouchHelper
DStickyHeaderItemDecoration
Attempts:
3 left
💡 Hint
Common Mistakes
Using DividerItemDecoration which only draws dividers, not sticky headers.
Confusing ItemTouchHelper with decorations.
3fill in blank
hard

Fix the error in the adapter method to provide header IDs for sticky headers.

Android Kotlin
override fun getHeaderId(position: Int): Long {
    return [1]
}
Drag options to blanks, or click blank then click option'
A-1L
B0L
Citems[position].headerId
Dposition.toLong()
Attempts:
3 left
💡 Hint
Common Mistakes
Returning position.toLong() which treats each item as a separate header.
Returning 0L which groups all items under one header.
4fill in blank
hard

Fill both blanks to implement the onDrawOver method for drawing sticky headers.

Android Kotlin
override fun onDrawOver(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
    val topChild = parent.getChildAt(0) ?: return
    val topChildPosition = parent.getChildAdapterPosition(topChild)
    val header = [1](parent, topChildPosition)
    val contactPoint = header.bottom
    val childInContact = parent.[2](parent.width / 2f, contactPoint)
    // Draw header logic here
}
Drag options to blanks, or click blank then click option'
AgetHeaderView
BfindChildViewUnder
CgetChildAt
DgetChildAdapterPosition
Attempts:
3 left
💡 Hint
Common Mistakes
Using getChildAt instead of findChildViewUnder for contact point detection.
Confusing getChildAdapterPosition with getHeaderView.
5fill in blank
hard

Fill all three blanks to complete the sticky header touch event handling.

Android Kotlin
override fun onInterceptTouchEvent(rv: RecyclerView, e: MotionEvent): Boolean {
    val headerView = rv.[1](e.x, e.y)
    if (headerView != null) {
        val position = rv.getChildAdapterPosition(headerView)
        [2](position)
        return true
    }
    return false
}

fun [3](position: Int) {
    // Handle header click
}
Drag options to blanks, or click blank then click option'
AfindChildViewUnder
BonHeaderClick
CnotifyHeaderClick
DhandleHeaderClick
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong method names for click handling.
Not checking if headerView is null before proceeding.