Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save PreyeaRegmi/6cf9073178d0365b5c37cfed1cd0e088 to your computer and use it in GitHub Desktop.
Save PreyeaRegmi/6cf9073178d0365b5c37cfed1cd0e088 to your computer and use it in GitHub Desktop.
verticalscroll-event-handler-compose
.pointerInput(Unit) {
detectVerticalDragGestures(
onDragStart = { offset ->
draggingItemIndex = (offset.y / itemHeight).toInt()
},
onDragEnd = {
coroutineScope.launch {
dragAmount.animateTo(0f, tween(200, easing = EaseInOut))
draggingItemIndex = -1
}
}
) { change, dragAmountDelta ->
coroutineScope.launch {
val currentOffset = scrollState.value
val maxOffset = scrollState.maxValue
//Prevent Scrolling Top
if (currentOffset == 0 && dragAmountDelta > 0) {
return@launch
}
//Prevent Scrolling Bottom
if (currentOffset == maxOffset && dragAmountDelta < 0) {
return@launch
}
scrollState.scrollBy(-dragAmountDelta)
val clampedDragAmountDelta = dragAmountDelta.coerceIn(-itemHeight, itemHeight)*.5f
//When Scrolled Down
if (dragAmountDelta > 0) {
dragAmount.snapTo((dragAmount.value - clampedDragAmountDelta))
}
//When Scrolled Up
else if (dragAmountDelta < 0) {
dragAmount.snapTo(dragAmount.value+ clampedDragAmountDelta)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment