前语
侧滑删去能够说是开发中常见的功能之一,话不多说直接看效果图。
思路
通过Box堆叠并加上拖动手势完成,伪代码如下:
Box {
Box()
Box(
modifier = Modifier
.graphicsLayer {
translationX = anchoredDraggableState.offset
}
)
}
拖动手势完成
AnchoredDraggable是compose提供的锚定状况的可拖动组件,咱们能够通过它来完成拖动。
val minBound = 0f //描点的方位
val maxBound = 100f //描点的方位
val anchoredDraggableState = remember(maxBound) {
AnchoredDraggableState(
initialValue = checked,
animationSpec = TweenSpec(durationMillis = 1000),
anchors = DraggableAnchors {
false at minBound
true at maxBound
},
positionalThreshold = { distance -> distance * 0.5f }, //触发方位的阈值,这儿设置0.5即中心方位时切换到下一个状况
velocityThreshold = { maxBound } //拖动速度有必要超越该阈值才干设置到下一个状况
)
}
完好代码
@OptIn(ExperimentalFoundationApi::class)
@Composable
fun SwipeBox(
checked: Boolean,
onCheckedChange: ((Boolean) -> Unit)?,
modifier: Modifier = Modifier,
bottomContent: @Composable BoxScope.() -> Unit,
content: @Composable BoxScope.() -> Unit
) {
var bottomWidth by remember { mutableFloatStateOf(0f) }
var bottomHeight by remember { mutableStateOf(0) }
val minBound = 0f
val maxBound = -bottomWidth
var forceAnimationCheck by remember { mutableStateOf(false) }
val anchoredDraggableState = remember(maxBound) {
AnchoredDraggableState(
initialValue = checked,
animationSpec = TweenSpec(durationMillis = 1000),
anchors = DraggableAnchors {
false at minBound
true at maxBound
},
positionalThreshold = { distance -> distance * 0.5f },
velocityThreshold = { maxBound }
)
}
val currentOnCheckedChange by rememberUpdatedState(onCheckedChange)
val currentChecked by rememberUpdatedState(checked)
LaunchedEffect(anchoredDraggableState) {
snapshotFlow { anchoredDraggableState.currentValue }
.collectLatest { newValue ->
if (currentChecked != newValue) {
currentOnCheckedChange?.invoke(newValue)
forceAnimationCheck = !forceAnimationCheck
}
}
}
LaunchedEffect(checked, forceAnimationCheck) {
if (checked != anchoredDraggableState.currentValue) {
anchoredDraggableState.animateTo(checked)
}
}
Box(
modifier = modifier
.anchoredDraggable(
state = anchoredDraggableState,
orientation = Orientation.Horizontal,
enabled = onCheckedChange != null
)
.clipToBounds()
) {
Box(
modifier = Modifier
.align(Alignment.CenterEnd)
.height(with(LocalDensity.current) {
bottomHeight.toDp()
})
.onSizeChanged {
bottomWidth = it.width.toFloat()
}
) {
bottomContent()
}
Box(
modifier = Modifier
.graphicsLayer {
translationX = anchoredDraggableState.offset
}
.onSizeChanged {
bottomHeight = it.height
}
) {
content()
}
}
}
怎么运用
var checked by remember { mutableStateOf(false) }
SwipeBox(
checked = checked,
onCheckedChange = { checked = it },
modifier = Modifier
.fillMaxWidth()
.wrapContentHeight(),
bottomContent = {
Row {
Box(
modifier = Modifier
.width(70.dp)
.fillMaxHeight()
.background(colorResource(R.color.blue))
) {
Text(
text = "标为未读",
modifier = Modifier.align(Alignment.Center),
style = TextStyle.Default.copy(
color = colorResource(R.color.white),
fontSize = 12.sp
)
)
}
Box(
modifier = Modifier
.width(70.dp)
.fillMaxHeight()
.background(colorResource(R.color.yellow))
) {
Text(
text = "不显示",
modifier = Modifier.align(Alignment.Center),
style = TextStyle.Default.copy(
color = colorResource(R.color.white),
fontSize = 12.sp
)
)
}
Box(
modifier = Modifier
.width(70.dp)
.fillMaxHeight()
.background(colorResource(R.color.red))
) {
Text(
text = "删去",
modifier = Modifier.align(Alignment.Center),
style = TextStyle.Default.copy(
color = colorResource(R.color.white),
fontSize = 12.sp
)
)
}
}
}
) {
Column(
modifier = Modifier
.fillMaxWidth()
.background(colorResource(R.color.white))
.padding(20.dp, 10.dp)
) {
Text(
text = "小美",
color = colorResource(R.color.text_333),
fontSize = 14.sp
)
Spacer(Modifier.size(5.dp))
Text(
text = "我的电脑坏了,你能过来看看嘛。",
color = colorResource(R.color.text_666),
fontSize = 12.sp
)
}
}
Thanks
以上就是本篇文章的全部内容,如有问题欢迎指出,咱们一起进步。
假如觉得本篇文章对您有帮助的话请点个赞让更多人看到吧,您的鼓舞是我行进的动力。
谢谢~~
源代码地址
推荐阅览
- Jetpack Compose : 从改造你的登录页面开始 – (juejin.cn)
- Jetpack Compose : 一学就会的自定义下拉改写&加载更多 – (juejin.cn)
- Jetpack Compose : 优雅的运用WebView – (juejin.cn)
- Jetpack Compose : 一文学会嵌套滚动NestedScrollConnection – (juejin.cn)
- Jetpack Compose : 超简略完成滚轮控件(WheelPicker) – (juejin.cn)
- Jetpack Compose : 超简略完成文本打开和收起 – (juejin.cn)