rememberPullRefreshState

Composable Function

Common
@Composable
@ExperimentalMaterialApi
fun rememberPullRefreshState(
    refreshing: Boolean,
    onRefresh: () -> Unit,
    refreshThreshold: Dp = PullRefreshDefaults.RefreshThreshold,
    refreshingOffset: Dp = PullRefreshDefaults.RefreshingOffset,
): PullRefreshState

Creates a PullRefreshState that is remembered across compositions.

Changes to refreshing will result in PullRefreshState being updated.

Parameters

refreshingA boolean representing whether a refresh is currently occurring.
onRefreshThe function to be called to trigger a refresh.
refreshThresholdThe threshold below which, if a release occurs, onRefresh will be called.
refreshingOffsetThe offset at which the indicator will be drawn while refreshing. This offset corresponds to the position of the bottom of the indicator.

Code Examples

PullRefreshSample

@Composable
@OptIn(ExperimentalMaterialApi::class)
fun PullRefreshSample() {
    val refreshScope = rememberCoroutineScope()
    var refreshing by remember { mutableStateOf(false) }
    var itemCount by remember { mutableStateOf(15) }
    fun refresh() =
        refreshScope.launch {
            refreshing = true
            delay(1500)
            itemCount += 5
            refreshing = false
        }
    val state = rememberPullRefreshState(refreshing, ::refresh)
    Box(Modifier.pullRefresh(state)) {
        LazyColumn(Modifier.fillMaxSize()) {
            if (!refreshing) {
                items(itemCount) { ListItem { Text(text = "Item ${itemCount - it}") } }
            }
        }
        PullRefreshIndicator(refreshing, state, Modifier.align(Alignment.TopCenter))
    }
}