gpt4 book ai didi

android - 手动拉入 PullToRefresh 不起作用(使用可复制示例)

转载 作者:行者123 更新时间:2023-12-03 07:59:33 25 4
gpt4 key购买 nike

我试图从 Material 库中实现 PullToRefresh 组件,但即使一对一复制 Google 自己的代码,滑动也不会触发任何内容。 https://developer.android.com/reference/kotlin/androidx/compose/material/pullrefresh/package-summary.html

我已经能够将他们的代码减少到最小的可复制示例。加载器将显示您是否手动将其设置为 true,但不会在滑动时触发。

View 不需要任何其他内容,例如,无论 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()
}

最佳答案

不确定什么不起作用,但基于此

Loader will show if you manually set it to true, but will not trigger on swiping.

我假设当您尝试将其下拉时,PullRefresh 没有显示。

基于docs ,

… The content needs to be 'vertically scrollable' for SwipeRefresh()to be able to react to swipe gestures. Layouts such as LazyColumn areautomatically vertically scrollable, but others such as Column orLazyRow are not. In those instances, you can provide aModifier.verticalScroll modifier…

只需向您的 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
)
}
}

enter image description here

关于android - 手动拉入 PullToRefresh 不起作用(使用可复制示例),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74829036/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com