Challenge - 5 Problems
Sticky Header Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2:00remaining
Sticky Header Behavior in RecyclerView
What will happen when you scroll the RecyclerView with this sticky header implementation?
Android Kotlin
class StickyHeaderDecoration : RecyclerView.ItemDecoration() { override fun onDrawOver(c: Canvas, parent: RecyclerView, state: RecyclerView.State) { val topChild = parent.getChildAt(0) ?: return val topChildPosition = parent.getChildAdapterPosition(topChild) if (topChildPosition == RecyclerView.NO_POSITION) return val header = getHeaderViewForItem(topChildPosition, parent) fixLayoutSize(header, parent) c.save() c.translate(0f, 0f) header.draw(c) c.restore() } private fun getHeaderViewForItem(position: Int, parent: RecyclerView): View { // returns header view for the item at position TODO() } private fun fixLayoutSize(view: View, parent: RecyclerView) { val widthSpec = View.MeasureSpec.makeMeasureSpec(parent.width, View.MeasureSpec.EXACTLY) val heightSpec = View.MeasureSpec.makeMeasureSpec(parent.height, View.MeasureSpec.UNSPECIFIED) view.measure(widthSpec, heightSpec) view.layout(0, 0, view.measuredWidth, view.measuredHeight) } }
Attempts:
2 left
💡 Hint
Think about what onDrawOver does in RecyclerView.ItemDecoration.
✗ Incorrect
The onDrawOver method draws the header view on top of the RecyclerView's children, making it appear fixed at the top while the list scrolls underneath.
🧠 Conceptual
intermediate1:30remaining
Purpose of fixLayoutSize in Sticky Header
Why is the fixLayoutSize method necessary when drawing sticky headers in RecyclerView?
Attempts:
2 left
💡 Hint
Think about what happens if a view is drawn without measuring and layout.
✗ Incorrect
Views must be measured and laid out before drawing to ensure they have correct width and height. fixLayoutSize does this for the header view.
❓ lifecycle
advanced2:00remaining
Handling Header Changes on Scroll
In a sticky header implementation, which RecyclerView callback is best to detect when the header should change as the user scrolls?
Attempts:
2 left
💡 Hint
Consider where you get notified about scroll position changes.
✗ Incorrect
OnScrollListener's onScrolled method is called whenever the RecyclerView scrolls, allowing you to check the current position and update the sticky header accordingly.
🔧 Debug
advanced2:00remaining
Sticky Header Overlapping Issue
You implemented sticky headers but notice the header overlaps the first list item partially. What is the most likely cause?
Attempts:
2 left
💡 Hint
Think about spacing between items and header.
✗ Incorrect
If item offsets are not adjusted to leave space for the sticky header, the first item will be drawn under the header causing overlap.
expert
2:30remaining
Sticky Header with Section Navigation
You want to add fast section navigation (like an index on the side) that scrolls the RecyclerView to the correct sticky header section. Which approach is best?
Attempts:
2 left
💡 Hint
Think about how RecyclerView scrolls to items by position.
✗ Incorrect
scrollToPosition scrolls the list so the item at the given position is visible, which works well to jump to a section's start and show its sticky header.