Skip to content

About

Sticky header for RecyclerView using ItemDecoration

Topics

Resources

Stars

1 star

Watchers

1 watching

Forks

Repository files navigation

StickyHeaderRecyclerViewSample

KO 외부 라이브러리 없이 RecyclerView.ItemDecoration만으로 Sticky Header를 구현한 샘플입니다. EN A sample that implements a sticky header in RecyclerView using only RecyclerView.ItemDecoration, with no external library.

📖 Blog / 블로그 · Android Sticky Header RecyclerView (2021.06) KO StickyHeaderItemDecoration 전체 코드와 동작 설명이 글에 있습니다. EN The full StickyHeaderItemDecoration code and a walkthrough are in the post.

Sticky header demo


How it works / 동작 방식

KO

  1. onDrawOver에서 RecyclerView 위에 헤더를 덧그립니다.
  2. 콜백(SectionCallback)으로 현재 맨 위 아이템의 헤더 뷰와, 헤더 여부를 받습니다.
  3. 다음 헤더가 닿으면 moveHeader로 밀어 올리고, 아니면 drawHeader로 항상 맨 위에 고정해서 그립니다.

EN

  1. onDrawOver draws the header on top of the RecyclerView.
  2. A SectionCallback provides the header view for the top item and tells whether a position is a header.
  3. When the next header touches it, moveHeader pushes it up; otherwise drawHeader pins it to the top.

Usage / 사용법

interface SectionCallback {
    fun isHeader(position: Int): Boolean
    fun getHeaderLayoutView(list: RecyclerView, position: Int): View?
}
recyclerView.addItemDecoration(
    StickyHeaderItemDecoration(object : StickyHeaderItemDecoration.SectionCallback {
        override fun isHeader(position: Int): Boolean = adapter.isHeader(position)
        override fun getHeaderLayoutView(list: RecyclerView, position: Int): View? =
            adapter.getHeaderView(list, position)
    })
)

KO isHeader / getHeaderView는 예제 앱의 SampleAdapter.kt에 구현되어 있고, 등록하는 코드는 MainActivity.kt에 있습니다. EN isHeader / getHeaderView are implemented in the sample's SampleAdapter.kt, and the decoration is registered in MainActivity.kt.


Files / 파일

File Description
StickyHeaderItemDecoration.kt KO 핵심 ItemDecoration · EN The core ItemDecoration
SampleAdapter.kt KO 헤더 여부와 헤더 뷰를 제공하는 어댑터 · EN Adapter that tells which items are headers and provides the header view
MainActivity.kt KO RecyclerView에 ItemDecoration 등록 · EN Registers the ItemDecoration on the RecyclerView
ListItemData.kt, SampleData.kt KO 목록 데이터 · EN List data

Requirements / 요구사항

  • minSdk 21, compileSdk/targetSdk 30
  • AndroidX RecyclerView (KO com.google.android.material:material:1.3.0을 통해 포함 · EN pulled in via com.google.android.material:material:1.3.0)

References / 참고 링크


📚 More on the blog / 블로그에서 더 보기

About

Sticky header for RecyclerView using ItemDecoration

Topics

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages