androidandroid-jetpack-composeandroid-jetpack-compose-list

There is something similar like swiperefreshlayout to pull to refresh in the LazyColumn jetpack compose


There is something similar like swiperefreshlayout to pull to refresh in the jetpack compose


Solution

  • The Google's Accompanist is now deprecated. Official support is now available in Compose Material from versin 1.3.0 use following code:

    The lines I marked with 1, 2, 3 does the job:

    val ptrState= 
       rememberPullRefreshState(viewState.isRefreshing, {viewModel.pullToRefresh()}) // 1
    
    Box(modifier = Modifier
        .fillMaxSize()
        .pullRefresh(ptrState)) { // 2
        LazyColumn(state = rememberLazyListState()) {
            items(
                items = viewState.YOUR_LIST_ITEMS,
                key = { item -> item.id },
                itemContent = {
                    YourItemCompose(it)
                })
        }
        PullRefreshIndicator
              (viewState.isRefreshing, ptrState, Modifier.align(Alignment.TopCenter)) // 3
    }
    

    viewState.isRefreshing is a boolean holding your refresh state and viewModel.pullToRefresh() is the method to do the actual work for refreshing the data (e.g. calling the API again)

    in case you don't know the dependency:

        implementation 'androidx.compose.material:material:1.4.0-alpha02'