kotlin 手动拉入PullToRefresh不起作用(使用可复制样本)

kdfy810k  于 2022-12-19  发布在  Kotlin
关注(0)|答案(1)|浏览(197)

我正尝试从素材库实现PullToRefresh组件,但即使1:1复制Google自己的代码,滑动也不会触发任何东西。https://developer.android.com/reference/kotlin/androidx/compose/material/pullrefresh/package-summary.html
我已经能够减少他们的代码,以最小的可复制的样本。加载程序将显示如果您手动设置为真,但不会触发刷卡。
视图不需要任何其他内容,例如,无论Box组件中还有什么,加载器都应该显示。

import androidx.compose.foundation.layout.*
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material.pullrefresh.PullRefreshIndicator
import androidx.compose.material.pullrefresh.pullRefresh
import androidx.compose.material.pullrefresh.rememberPullRefreshState
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview

@OptIn(ExperimentalMaterialApi::class)
@Composable
fun PullToRefreshComposableTEST() {

    var loading by remember {
        mutableStateOf(true)
    }

    val pullRefreshState = rememberPullRefreshState(
        refreshing = loading,
        onRefresh = {
            loading = true
        })

    Box(
        Modifier.fillMaxSize().pullRefresh(pullRefreshState)
    ) {
        PullRefreshIndicator(
            modifier = Modifier.align(Alignment.TopCenter),
            refreshing = loading,
            state = pullRefreshState
        )
    }
}

@Preview(showSystemUi = true)
@Composable
private fun Preview() {
    PullToRefreshComposableTEST()
}
cyvaqqii

cyvaqqii1#

不确定什么不起作用,但基于此
加载器将显示您是否手动设置为真,但不会触发刷卡。
我假设PullRefresh在您尝试将其下拉时不会显示。
基于docs
...内容需要“可垂直滚动”,SwipeRefresh()才能对滑动手势做出React。LazyColumn等布局可以自动垂直滚动,但Column或LazyRow等布局不能。在这些情况下,您可以提供修饰符。verticalScroll修饰符...
只需在Box上添加一个垂直滚动条,即可将其下拉
您修改的代码:

@OptIn(ExperimentalMaterialApi::class)
@Composable
fun PullToRefreshComposableTEST() {

    var loading by remember {
        mutableStateOf(false)
    }

    val pullRefreshState = rememberPullRefreshState(
        refreshing = loading,
        onRefresh = {
            loading = !loading
        })

    Box(
        Modifier
            .fillMaxSize()
            .pullRefresh(pullRefreshState)
            .verticalScroll(rememberScrollState())
    ) {
        PullRefreshIndicator(
            modifier = Modifier.align(Alignment.TopCenter),
            refreshing = loading,
            state = pullRefreshState
        )
    }
}

相关问题