How to achieve pull-to-refresh a list view in comp...
# compose
a
How to achieve pull-to-refresh a list view in compose ?
👍 3
m
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 🙂
v
@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.
m
Oh sorry, I wan't talking about using existent SwipeToReshesh, I was talking about making your own 🙂
v
Ohh Okay. Got it thanks.
m
With compose it should be very simple to make a workable version
Copy code
@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()
                }
            }
        }
    }
}
👍 2
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
v
Thanks @matvei 🤩. Will definitely look into this.
r
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 😅