最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

android - Offset Not Working in Swiper Element - Jetpack Compose - Stack Overflow

programmeradmin1浏览0评论

I am trying to make a swiper element in my app, one that behaves like the swiper Apple phones use when picking up a call. In this process, I have used pointer input and offset to acquire the user action as well as to update the visuals. However, even when I hardcoded the offset, the icon doesnt budge. Its a circle in a rounded rectangle that should be swiping sideways.

Box(
    Modifier
        .padding(top = 40.dp)
        .width(350.dp)
        .height(60.dp)
        .align(Alignment.CenterHorizontally)
        .clip(RoundedCornerShape(45.dp))
        .background(White.copy(0.4f))
) {

    // Slider Circle
    val density= LocalDensity.current
    val maxOffset=with(density){280.dp.toPx()}
    var offsetX by remember {mutableFloatStateOf(0f)

    Box(
        Modifier
            .size(60.dp)
            .padding(6.dp)
            .clip(RoundedCornerShape(45.dp))
            .background(White)
            // Set the offset when scrolled (Does NOT WORK)
            .offset (with(density){(offsetX/density.density).dp}, 0.dp)
            .pointerInput(Unit) {
                 detectDragGestures { change, dragAmount ->
                     change.consume() //Handles the Drag event

                     //Limit the X
                     offsetX=(dragAmount.x+offsetX).coerceIn(0f,maxOffset)
                     Log.i("Slider","$offsetX")
                }
            } 
            .zIndex(1f)
        )
}

I used logcat to ensure that the offset was correct, which it was. I clamped it and used density to ensure it doesn't go out of bounds. However, the icon just didn't move.

I am trying to make a swiper element in my app, one that behaves like the swiper Apple phones use when picking up a call. In this process, I have used pointer input and offset to acquire the user action as well as to update the visuals. However, even when I hardcoded the offset, the icon doesnt budge. Its a circle in a rounded rectangle that should be swiping sideways.

Box(
    Modifier
        .padding(top = 40.dp)
        .width(350.dp)
        .height(60.dp)
        .align(Alignment.CenterHorizontally)
        .clip(RoundedCornerShape(45.dp))
        .background(White.copy(0.4f))
) {

    // Slider Circle
    val density= LocalDensity.current
    val maxOffset=with(density){280.dp.toPx()}
    var offsetX by remember {mutableFloatStateOf(0f)

    Box(
        Modifier
            .size(60.dp)
            .padding(6.dp)
            .clip(RoundedCornerShape(45.dp))
            .background(White)
            // Set the offset when scrolled (Does NOT WORK)
            .offset (with(density){(offsetX/density.density).dp}, 0.dp)
            .pointerInput(Unit) {
                 detectDragGestures { change, dragAmount ->
                     change.consume() //Handles the Drag event

                     //Limit the X
                     offsetX=(dragAmount.x+offsetX).coerceIn(0f,maxOffset)
                     Log.i("Slider","$offsetX")
                }
            } 
            .zIndex(1f)
        )
}

I used logcat to ensure that the offset was correct, which it was. I clamped it and used density to ensure it doesn't go out of bounds. However, the icon just didn't move.

Share Improve this question asked 9 hours ago Wen ZheWen Zhe 231 silver badge4 bronze badges 0
Add a comment  | 

1 Answer 1

Reset to default 4

Please have a look at the offset Modifier documentation:

Offset the content by offset px

As the inner Box Composable has no actual content, the offset Modifier has nothing it can move around.

You can directly add the offset Modifier to the parent Box instead.
Also, you can simply use the draggable Modifier instead of the pointerInput like this:

@Composable
fun SwiperDemo() {

    val density = LocalDensity.current
    val maxOffset = with(density){ 290.dp.toPx() }
    var offsetX by remember { mutableFloatStateOf(0f) }

    Box(
        Modifier
            .padding(top = 40.dp)
            .width(350.dp)
            .height(60.dp)
            .clip(RoundedCornerShape(45.dp))
            .background(Green.copy(0.4f))
            .offset { IntOffset(offsetX.roundToInt(), 0) }  // apply on parent Box
    ) {

        Box(
            Modifier
                .size(60.dp)
                .padding(6.dp)
                .clip(RoundedCornerShape(45.dp))
                .background(Green)
                .draggable(
                    orientation = Orientation.Horizontal,
                    state = rememberDraggableState { delta ->
                        val localOffset = offsetX + delta
                        offsetX = localOffset.coerceIn(0f, maxOffset)
                    }
                )
        )
    }
}

Output:

发布评论

评论列表(0)

  1. 暂无评论