https://kotlinlang.org logo
a

amar_1995

03/10/2020, 8:08 PM
How to achieve pull-to-refresh a list view in compose ?
👍 3
m

matvei

03/11/2020, 9:43 PM
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

vipulasri

03/12/2020, 5:14 AM
@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

matvei

03/12/2020, 10:38 AM
Oh sorry, I wan't talking about using existent SwipeToReshesh, I was talking about making your own 🙂
v

vipulasri

03/12/2020, 10:40 AM
Ohh Okay. Got it thanks.
m

matvei

03/12/2020, 10:46 AM
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

vipulasri

03/12/2020, 10:47 AM
Thanks @matvei 🤩. Will definitely look into this.
r

Ryan Mentley

03/13/2020, 8:22 AM
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 😅
2 Views