Search code examples
androidandroid-jetpack-composeandroid-jetpack-compose-lazy-column

How to implement swipe to refresh in Jetpack compose


How to create swipe to refresh in Jetpack compose using kotlin? Please Share proper reference link

SwipeRefresh is not available

 SwipeRefresh(
        state = rememberSwipeRefreshState(isRefreshing),
        onRefresh = {  },
    ) {
        LazyColumn {
           
        }
    }

Solution

  • To create a swipe-to-refresh layout, we need to add dependency in buld.gradle which will provide swipe to refresh layout just like SwipeRefreshLayout in traditional android.

      implementation 'com.google.accompanist:accompanist-swiperefresh:0.24.13-rc'
    

    ..

    To create this kind of layout we require two APIs one SwipeRefresh for layout and another rememberSwipeRefreshState which will remember the state.

    @Composable
    fun SwipeRefreshCompose() {
    
        var refreshing by remember { mutableStateOf(false) }
        LaunchedEffect(refreshing) {
            if (refreshing) {
                delay(3000)
                refreshing = false
            }
        }
    
        SwipeRefresh(
            state = rememberSwipeRefreshState(isRefreshing = refreshing),
            onRefresh = { refreshing = true },
        ) {
    
           // list view
    
        }
    
    }