2024 最新版 Compose material3 下拉刷新,版本 >= 1.2.0 的 compose material3 已经更新了下拉刷新组件的 Api
见 https://developer.android.com/jetpack/androidx/releases/compose-material3?hl=zh-cn#1.2.0
使用方法:https://developer.android.com/reference/kotlin/androidx/compose/material3/pulltorefresh/PullToRefreshState
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material3.LinearProgressIndicator
import androidx.compose.material3.ListItem
import androidx.compose.material3.Text
import androidx.compose.material3.pulltorefresh.rememberPullToRefreshState
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.input.nestedscroll.nestedScrollvar itemCount by remember { mutableStateOf(15) }
val state = rememberPullToRefreshState()
if (state.isRefreshing) {LaunchedEffect(true) {// fetch somethingdelay(1500)itemCount += 5state.endRefresh()}
}
Box(Modifier.nestedScroll(state.nestedScrollConnection)) {LazyColumn(Modifier.fillMaxSize()) {if (!state.isRefreshing) {items(itemCount) {ListItem({ Text(text = "Item ${itemCount - it}") })}}}PullToRefreshContainer(state = pullRefreshState, modifier = Modifier.align(Alignment.TopCenter))
// 或者
// if (state.isRefreshing) {
// LinearProgressIndicator()
// } else {
// LinearProgressIndicator(progress = { state.progress })
// }
}