Created
June 14, 2024 19:55
-
-
Save PreyeaRegmi/6cf9073178d0365b5c37cfed1cd0e088 to your computer and use it in GitHub Desktop.
verticalscroll-event-handler-compose
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.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