Jetpack Compose学习 Swipeable

Jetpack Compose学习 -------- Swipeable 滑动

Swipeable 滑动 基本使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54


@Composable
fun ComposableSample() {
var blockSize = 48.dp
var blockSizePx = with(LocalDensity.current) { blockSize.toPx() }
var swipeableState = rememberSwipeableState(initialValue = Status.CLOSE)
var anchors = mapOf(
0f to Status.CLOSE,
blockSizePx to Status.OPEN
)
Box(
modifier = Modifier
.size(
height = blockSize,
width = blockSize * 2
)
.background(Color.LightGray)
) {
Box(
modifier = Modifier
//滑动
.swipeable(
state = swipeableState,
//锚点,可以通过锚点设置在不同状态时所应该对应的偏移量信息
anchors = anchors,
//常用作定制不同锚点间吸附效果的临界阈值
//常用有 FixedThreshold(Dp) 和 FractionalThreshold(Float)等
thresholds = { from, to ->
if (from == Status.CLOSE) {
FractionalThreshold(0.3f)
} else {
FractionalThreshold(0.5f)
}
},
//手势方向,被修饰组件的手势方向只能是水平或垂直
orientation = Orientation.Horizontal,
)
//移动
.offset {
//整体移动
IntOffset(swipeableState.offset.value.toInt(), 0)
}
.size(blockSize)
.background(Color.DarkGray)
)
}
}

enum class Status {
CLOSE, OPEN
}