androidx.compose.material3.pulltorefresh

PullToRefreshBox

Composable function

Pull to refresh lets the user reload a list by pulling it down from the top. An indicator shows while it loads.

Wrap a scrollable list in a PullToRefreshBox. Keep a Boolean for "loading" yourself:

  1. In onRefresh, set it to true and start loading.
  2. When loading is done, set it back to false.
A list while refreshing
A list while refreshing
PullToRefreshBox(
    isRefreshing = true, // normally your state, true while loading
    onRefresh = { /* start loading */ },
    modifier = Modifier.size(360.dp, 260.dp),
) {
    LazyColumn(Modifier.fillMaxSize()) {
        items(5) { i -> ListItem(headlineContent = { Text("Message ${i + 1}") }) }
    }
}

Parameters

@Composable
fun PullToRefreshBox(
    isRefreshing: Boolean,
    onRefresh: () -> Unit,
    modifier: Modifier = Modifier,
    state: PullToRefreshState = rememberPullToRefreshState(),
    contentAlignment: Alignment = Alignment.TopStart,
    indicator: @Composable BoxScope.() -> Unit = { Indicator(modifier = Modifier.align(Alignment.TopCenter), isRefreshing = isRefreshing, state = state) },
    enabled: Boolean = true,
    threshold: Dp = PullToRefreshDefaults.PositionalThreshold,
    content: @Composable BoxScope.() -> Unit,
)

isRefreshing: BooleanRequired

Whether content is loading. The indicator shows while this is true. Pass your state here.

onRefresh: () -> UnitRequired

Called when the user pulls far enough and lets go. Start loading here.

modifier: ModifierOptional

Default: Modifier

Changes size, padding, or position. See the Modifier guide.

state: PullToRefreshStateOptional

Default: rememberPullToRefreshState()

The pull distance. Create it with rememberPullToRefreshState(). Needed only for a custom indicator.

contentAlignment: AlignmentOptional

Default: Alignment.TopStart

How the content lines up inside the box.

indicator: @Composable BoxScope.() -> UnitOptional

Default: { Indicator(modifier = Modifier.align(Alignment.TopCenter), isRefreshing = isRefreshing, state = state) }

The spinner shown while pulling and loading. Default: PullToRefreshDefaults.Indicator. For an Expressive look, use PullToRefreshDefaults.LoadingIndicator.

enabled: BooleanOptionalSince 1.11

Default: true

Set to false to turn off pulling.

threshold: DpOptionalSince 1.11

Default: PullToRefreshDefaults.PositionalThreshold

How far the user must pull before letting go starts a refresh.

content: @Composable BoxScope.() -> UnitRequired

The list. It must be scrollable, like a LazyColumn.

Longer Example

Pull down to load new messages. While loading, isRefreshing is true and the indicator spins.

Pull down to load new messages
Pull down to load new messages
val messages = remember { mutableStateListOf("Lunch on Friday?", "Your order has shipped", "Meeting notes") }
var refreshing by remember { mutableStateOf(false) }
val scope = rememberCoroutineScope()

PullToRefreshBox(
    isRefreshing = refreshing,
    onRefresh = {
        scope.launch {
            refreshing = true
            delay(1500)                         // in an app: load from the network
            messages.add(0, "New message ${messages.size + 1}")
            refreshing = false
        }
    },
    modifier = Modifier.size(360.dp, 260.dp),
) {
    // The list must fill the box and be scrollable, even when it is short,
    // or the pull gesture can't start.
    LazyColumn(Modifier.fillMaxSize()) {
        items(messages, key = { it }) { message ->
            ListItem(headlineContent = { Text(message) }, modifier = Modifier.animateItem())
        }
    }
}