'How to specify Column item offset in Jetpack Compose (Like ItemDecoration in RecyclerView)
I'm working with a Jetpack Compose Column; Same as for the stickyHeaders, I need to define a "stickyFooter" for the same item. So: When the item is inside the screen, it scrolls with other items normally; If the item would leave the screen from top, it must act as stickyHeader; If the item would leave the screen from bottom, it should act as "stickyFooter".
I need something to implement this behavior. Below, the code i wrote for RecyclerView that does this exact thing:
class StickyControlsItemDecoration : ItemDecoration() {
override fun getItemOffsets(
outRect: Rect,
view: View,
parent: RecyclerView,
state: RecyclerView.State
) {
val position = parent.getChildAdapterPosition(view)
val viewType = parent.adapter?.getItemViewType(position) ?: 0
if (!viewType.isSticky()) {
return
}
parent.setOnScrollChangeListener { _, _, _, _, _ ->
val topMaxPosition = parent.height - view.height - parent.paddingBottom
val itemTop = view.top.toFloat() + view.marginBottom
when {
itemTop > topMaxPosition -> {
view.translationY = topMaxPosition - itemTop
}
itemTop < 0 -> {
view.translationY = -itemTop
}
else -> {
view.translationY = 0f
}
}
}
}
}
I tried with measurable Layout, but it doesn't work on scroll.
Layout(
content = {/*THINGS*/}
) { measurables, constraints ->
/*apparently useless code*/
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
