Android Jetpack Compose material3 rememberPullRefreshState unresolved reference
val isRefreshing by viewModel.isRefreshing.collectAsStateWithLifecycle()
val pullRefreshState = rememberPullRefreshState(refreshing, { viewModel.refresh() })
I have found the solution. The compose material3 version >= 1.2.0 has updated the API for the pull-to-refresh component.
See https://developer.android.com/jetpack/androidx/releases/compose-material3#1.2.0
Usage: 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.nestedScroll
var itemCount by remember { mutableStateOf(15) }
val state = rememberPullToRefreshState()
if (state.isRefreshing) {
LaunchedEffect(true) {
// fetch something
delay(1500)
itemCount += 5
state.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))
// or
// if (state.isRefreshing) {
// LinearProgressIndicator()
// } else {
// LinearProgressIndicator(progress = { state.progress })
// }
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With