PullToRefreshState



The state that is associated with a PullToRefreshContainer. Each instance of PullToRefreshContainer should have its own PullToRefreshState.

PullToRefreshState can be used with other progress indicators like so:

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Refresh
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.LinearProgressIndicator
import androidx.compose.material3.ListItem
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.material3.pulltorefresh.PullToRefreshState
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.Modifier
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()
    }
}
Scaffold(
    modifier = Modifier.nestedScroll(state.nestedScrollConnection),
    topBar = {
        TopAppBar(
            title = { Text("TopAppBar") },
            // Provide an accessible alternative to trigger refresh.
            actions = {
                IconButton(onClick = { state.startRefresh() }) {
                    Icon(Icons.Filled.Refresh, "Trigger Refresh")
                }
            }
        )
    }
) {
    Box(Modifier.padding(it)) {
        LazyColumn(Modifier.fillMaxSize()) {
            if (!state.isRefreshing) {
                items(itemCount) {
                    ListItem({ Text(text = "Item ${itemCount - it}") })
                }
            }
        }
        if (state.isRefreshing) {
            LinearProgressIndicator()
        } else {
            LinearProgressIndicator(progress = { state.progress })
        }
    }
}

Summary

Public functions

Unit

Sets isRefreshing to false.

Cmn
Unit

Sets isRefreshing to true.

Cmn

Public properties

Boolean

Indicates whether a refresh is occurring.

Cmn
NestedScrollConnection

A NestedScrollConnection that should be attached to a Modifier.nestedScroll in order to keep track of the scroll events.

Cmn
Float

The threshold (in pixels), above which if a release occurs, a refresh will be called

Cmn
Float

PullRefresh progress towards positionalThreshold.

Cmn
Float

The vertical offset (in pixels) for the PullToRefreshContainer to consume

Cmn

Public functions

endRefresh

fun endRefresh(): Unit

Sets isRefreshing to false.

startRefresh

fun startRefresh(): Unit

Sets isRefreshing to true.

Public properties

isRefreshing

val isRefreshingBoolean

Indicates whether a refresh is occurring.

nestedScrollConnection

var nestedScrollConnectionNestedScrollConnection

A NestedScrollConnection that should be attached to a Modifier.nestedScroll in order to keep track of the scroll events.

positionalThreshold

val positionalThresholdFloat

The threshold (in pixels), above which if a release occurs, a refresh will be called

progress

val progressFloat

PullRefresh progress towards positionalThreshold. 0.0 indicates no progress, 1.0 indicates complete progress, 1.0 indicates overshoot beyond the provided threshold

verticalOffset

val verticalOffsetFloat

The vertical offset (in pixels) for the PullToRefreshContainer to consume