StickyItemDecoration

device-2022-05-31-141030.mp4

Usage

It's not necessary to create a sticky item as RecyclerView is initializing. You can add it after you create an adapter and submit some items, because it's just an item decoration.

First, create a view and bind some data with it. Create an item decoration and add it to the recyclerView wherever you want.

val view = layoutInflater.inflate(R.layout.view_holder, recyclerView, false)
val stickyView = ViewHolder(view).apply { bind(stickyItem) }.itemView
val stickyItemDecoration = StickyItemDecoration(stickyView, stickyItem.adapterPosition)
recyclerView.addItemDecoration(stickyItemDecoration)