coolchandrakumar
05/18/2020, 1:44 PMval modifier = Modifier.rawDragGestureFilter(dragObserver = object : DragObserver { })
Button( onClick = {
log()
}) {
Text(text = "Delete")
}
Box( modifier = modifier.drawLayer(
translationX = itemLeft.value
)
) {
}
Button click not working !!! translationX not moving the actual UI coordinates !?Andrey Kulikov
05/18/2020, 2:54 PMcoolchandrakumar
05/18/2020, 3:01 PMTimo Drick
05/18/2020, 8:05 PMModifier.offset(x = xOffset, y = 0.dp)
Also you should consider using
Modifier.draggable
Instead of rawDragGestureFiltercoolchandrakumar
05/19/2020, 5:13 AMDraw the content into a layer
So I put another layout composable over the Swipe Item now working.Andrey Kulikov
05/19/2020, 10:44 AMTimo Drick
05/19/2020, 4:30 PMVerticalScroller() {
Column(Modifier.padding(10.dp)) {
for (i in 0..50) {
SwipeArea(onSwiped = { log("Swiped $it") }, background = {
IconButton(modifier = Modifier.padding(10.dp), onClick = { log("Test") }) {
Icon(asset = Icons.Default.Bookmark)
}
}) {
Box(modifier = Modifier.fillMaxWidth().preferredHeight(100.dp), backgroundColor = if (i % 2 == 0) Color.LightGray else Color.Gray) {
Text("Test message $i")
}
}
}
}
}
@Composable
fun SwipeArea(modifier: Modifier = Modifier, onSwiped: (Boolean) -> Unit, background: @Composable() () -> Unit = { Box(
Modifier.fillMaxWidth()) }, children: @Composable() () -> Unit = emptyContent()
) {
val position = animatedFloat(0f)
var flingConfig = AnchorsFlingConfig(listOf(0f, 0f))
var isSwiped by state { false }
Stack(modifier = modifier.draggable(
startDragImmediately = position.isRunning,
dragDirection = DragDirection.Horizontal,
onDragStopped = {
position.fling(flingConfig, it)
},
onDragDeltaConsumptionRequested = { delta ->
position.snapTo(position.value + delta)
delta
}
)) {
val xOffset = with(DensityAmbient.current) { position.value.toDp() }
Box(modifier = Modifier.gravity(Alignment.CenterEnd).onChildPositioned {
val width = it.size.width.value.toFloat()
flingConfig = AnchorsFlingConfig(anchors = listOf(-width,0f), onAnimationEnd = { _,endValue,_ ->
log("Fling end $endValue")
isSwiped = endValue != 0f
onSwiped(isSwiped)
})
position.setBounds(-width, 0f)
}) {
background()
}
Clickable(onClick = {}, enabled = !isSwiped) {
Box(
Modifier.offset(x = xOffset, y = 0.dp).zIndex(1f)
) {
children()
}
}
}
}