Thread
#compose
    amar_1995

    amar_1995

    2 years ago
    How to achieve pull-to-refresh a list view in compose ?
    matvei

    matvei

    2 years ago
    Not sure it's possible right now, as AdapterList doesn't expose scroll state. You can certainly achieve this with with Vectical/HorizontalScroller, try it 🙂
    vipulasri

    vipulasri

    2 years ago
    @matvei but I would like to know how to use it? I mean use it directly in xml? just because
    swipe-to-refresh
    doesn't support dsl as of now.
    matvei

    matvei

    2 years ago
    Oh sorry, I wan't talking about using existent SwipeToReshesh, I was talking about making your own 🙂
    vipulasri

    vipulasri

    2 years ago
    Ohh Okay. Got it thanks.
    matvei

    matvei

    2 years ago
    With compose it should be very simple to make a workable version
    @Composable
        fun SwipeToRefreshLayout(
            resheshingState: Boolean,
            onRefresh: () -> Unit,
            swipeIcon: @Composable() () -> Unit,
            content: @Composable() () -> Unit
        ) {
            val size = with(DensityAmbient.current) { 100.dp.toPx().value }
            //min is below - to hide
            val min = -size
            val max = size * 2
            StateDraggable(
                state = resheshingState,
                onStateChange = { if (it) onRefresh() },
                anchorsToState = listOf(min to false, max to true),
                animationBuilder = TweenBuilder(),
                dragDirection = DragDirection.Vertical,
                minValue = min,
                maxValue = max
            ) { value ->
                val dpOffset = with(DensityAmbient.current) {
                    (value.value * 0.5).px.toDp()
                }
                Stack {
                    content()
                    Box(LayoutGravity.TopCenter + LayoutOffset(0.dp, dpOffset)) {
                        swipeIcon()
                    }
                }
            }
        }
    }
    I made one few month ago just for fun, should still work. You need to copy-paste StateDraggable for now though, as it's not public
    vipulasri

    vipulasri

    2 years ago
    Thanks @matvei 🤩. Will definitely look into this.
    r

    Ryan Mentley

    2 years ago
    It miiiiiiiight work with AdapterList, assuming the nested drag stuff works as intended. I haven't tried it though, currently have more basic things to add support for 😅