Sticky headers help keep section titles visible while you scroll through a list. This makes it easier to know which group of items you are looking at.
Sticky headers in Android Kotlin
class MyAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder>() { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder { /* create view holder */ } override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) { /* bind data */ } override fun getItemCount(): Int { /* return item count */ } // Implement sticky header logic using ItemDecoration or libraries }
Sticky headers are usually implemented with RecyclerView and ItemDecoration.
You can also use third-party libraries to simplify sticky header creation.
val recyclerView = findViewById<RecyclerView>(R.id.recyclerView) recyclerView.adapter = MyAdapter() recyclerView.addItemDecoration(StickyHeaderDecoration())
class StickyHeaderDecoration : RecyclerView.ItemDecoration() {
override fun onDrawOver(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
// Draw header that sticks at top
}
}This example shows a simple list of fruits grouped by their first letter. The sticky header stays visible at the top as you scroll through the list.
class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val recyclerView = findViewById<RecyclerView>(R.id.recyclerView) recyclerView.layoutManager = LinearLayoutManager(this) recyclerView.adapter = SimpleAdapter(getSampleData()) recyclerView.addItemDecoration(SimpleStickyHeaderDecoration(recyclerView.adapter as SimpleAdapter)) } private fun getSampleData(): List<Pair<String, String>> { return listOf( "A" to "Apple", "A" to "Avocado", "B" to "Banana", "B" to "Blueberry", "C" to "Cherry", "C" to "Coconut" ) } } class SimpleAdapter(private val items: List<Pair<String, String>>) : RecyclerView.Adapter<SimpleAdapter.ViewHolder>() { class ViewHolder(view: View) : RecyclerView.ViewHolder(view) { val textView: TextView = view.findViewById(android.R.id.text1) } override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { val view = LayoutInflater.from(parent.context).inflate(android.R.layout.simple_list_item_1, parent, false) return ViewHolder(view) } override fun onBindViewHolder(holder: ViewHolder, position: Int) { holder.textView.text = items[position].second } override fun getItemCount(): Int = items.size fun getHeaderForPosition(position: Int): String = items[position].first } class SimpleStickyHeaderDecoration(private val adapter: SimpleAdapter) : RecyclerView.ItemDecoration() { private val paint = Paint().apply { color = Color.LTGRAY textSize = 48f isAntiAlias = true } override fun onDrawOver(c: Canvas, parent: RecyclerView, state: RecyclerView.State) { val left = parent.paddingLeft.toFloat() val right = (parent.width - parent.paddingRight).toFloat() if (parent.childCount == 0) return val child = parent.getChildAt(0) val position = parent.getChildAdapterPosition(child) if (position == RecyclerView.NO_POSITION) return val header = adapter.getHeaderForPosition(position) val top = parent.paddingTop.toFloat() paint.color = Color.LTGRAY c.drawRect(left, top, right, top + 60f, paint) paint.color = Color.BLACK c.drawText(header, left + 20f, top + 45f, paint) } }
Sticky headers improve user experience by showing context while scrolling.
Implementing sticky headers requires careful drawing on RecyclerView's canvas.
Test on different screen sizes to ensure headers display correctly.
Sticky headers keep section titles visible during scrolling.
Use RecyclerView with ItemDecoration to create sticky headers.
Custom drawing is needed to show headers that stick at the top.
Practice
Solution
Step 1: Understand sticky header behavior
Sticky headers keep section titles visible at the top as you scroll through grouped list items.Step 2: Compare with other options
Animations, image loading, and sorting are unrelated to sticky headers.Final Answer:
To keep section titles visible while scrolling -> Option AQuick Check:
Sticky headers = visible section titles [OK]
- Confusing sticky headers with animations
- Thinking sticky headers sort the list
- Assuming sticky headers load images
Solution
Step 1: Identify components for sticky headers
RecyclerView supports ItemDecoration which allows custom drawing for sticky headers.Step 2: Eliminate other options
ListView and ScrollView do not support sticky headers natively; ViewPager is for paging, not lists.Final Answer:
RecyclerView with ItemDecoration -> Option AQuick Check:
Sticky headers use RecyclerView + ItemDecoration [OK]
- Choosing ListView instead of RecyclerView
- Using ScrollView which lacks sticky header support
- Confusing ViewPager with list components
override fun onDrawOver(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
val child = parent.getChildAt(0) ?: return
val position = parent.getChildAdapterPosition(child)
val header = getHeaderViewForItem(position)
c.save()
c.translate(0f, 0f)
header.draw(c)
c.restore()
}
What is the visible effect of this code when scrolling?Solution
Step 1: Analyze onDrawOver behavior
onDrawOver draws the header on top of the RecyclerView content, fixed at the top (0f, 0f).Step 2: Understand scrolling effect
Since header is drawn at fixed position, it stays visible while list scrolls underneath.Final Answer:
The header stays fixed at the top while list scrolls underneath -> Option BQuick Check:
onDrawOver + translate(0,0) = sticky header visible [OK]
- Thinking header scrolls away with items
- Assuming header appears at bottom
- Believing header blinks on scroll
Solution
Step 1: Identify flicker cause in sticky headers
Repeatedly creating and measuring header views without caching causes flicker during fast scroll.Step 2: Check other options
Layout manager type or calling super.onDrawOver() usually don't cause flicker; wrap_content affects layout size but not flicker.Final Answer:
Not caching header views causing repeated layout calls -> Option DQuick Check:
Missing header caching = flicker [OK]
- Ignoring header view caching
- Blaming layout manager type
- Forgetting super call but unrelated to flicker
Solution
Step 1: Understand smooth sticky header transition
Drawing current header at top and moving it up as next header scrolls in creates smooth push effect.Step 2: Evaluate other options
Replacing header fully causes flicker; separate TextView won't sync scrolling; disabling animations doesn't create smooth transition.Final Answer:
Draw the current header at top and translate it up as next header approaches -> Option CQuick Check:
Smooth sticky header = translate current header up [OK]
- Replacing header view causing flicker
- Using fixed TextView outside RecyclerView
- Disabling animations instead of translating header
Ready to prove it?
Pass the test and this pattern moves unknown
