Aandroid Compose 侧滑删除按钮

//dp转px
val Int.toPx: Float
    get() {
        var scale = 1f
        context.resources.displayMetrics.apply {
            scale = density
        }
        return this * scale
    }

@Preview
@Composable
fun TestUI(){
    Column(Modifier.fillMaxSize(),verticalArrangement = Arrangement.Center) {
        SwipeToDeleteItem(
            content = {
                Row(
                    modifier = Modifier.height(30.dp)
                        .padding(12.dp)
                ) {

                }
            },
            onDelete = {

            }
        )
    }
}


@Composable
fun SwipeToDeleteItem(
    content: @Composable (RowScope.() -> Unit),
    onDelete: () -> Unit
) {
    val swipeState = remember { Animatable(0f) }
    val coroutineScope = rememberCoroutineScope()
    val swipeThreshold = (-60).toPx
    val focusRequester = remember { FocusRequester() }

    Box(
        modifier = Modifier
            .focusRequester(focusRequester)
            .onFocusChanged {
                if (!it.isFocused) {
                    coroutineScope.launch {
                        swipeState.animateTo(0f, animationSpec = tween(300))
                    }
                }
            }
            .focusable()
            .fillMaxWidth()
            .padding(12.dp)
            .clip(RoundedCornerShape(12.dp))
            .height(60.dp),
        contentAlignment = Alignment.CenterEnd,
    ) {
        Box(
            modifier = Modifier
                .fillMaxSize()
                .background(color = Color(0xFFD54941)),
            contentAlignment = Alignment.CenterEnd
        ) {
            Text(
                "删除",
                modifier = Modifier
                    .width(60.dp)
                    .throttleClick {
                        coroutineScope.launch {
                            swipeState.animateTo(0f, animationSpec = tween(300))
                        }
                        onDelete()
                    },
                textAlign = TextAlign.Center,
                style = TextStyle(color = Color(0xFFFFFFFF))
            )
        }
        Row(
            modifier = Modifier
                .offset {
                    IntOffset(swipeState.value.toInt(), 0)
                }
                .fillMaxSize()
                .background(Color(0xFFFFFFFF))
                .draggable(
                    orientation = Orientation.Horizontal,
                    state = rememberDraggableState { delta ->
                        coroutineScope.launch {
                            if (delta < 0 || swipeState.value + delta < 0) {
                                swipeState.snapTo(swipeState.value + delta)
                            }
                        }
                    },
                    onDragStopped = {
                        coroutineScope.launch {
                            focusRequester.requestFocus()
                            if (swipeState.value < swipeThreshold) {
                                swipeState.animateTo(swipeThreshold, animationSpec = tween(300))
                            } else {
                                swipeState.animateTo(0f, animationSpec = tween(300))
                            }
                        }
                    }
                ),
            content = content
        )
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值