Android Compose pullRefresh下拉刷新demo

Android Compose pullRefresh下拉刷新组件使用,可添加依赖包:

implementation("androidx.compose.material:material")

核心代码:

/**
 * 下拉刷新
 * @param onRefreshing 下拉刷新时数据组装,可以请求接口
 * @param showContent  显示数据
 */
@OptIn(ExperimentalMaterialApi::class)
@Composable
fun refreshData(onRefreshing: () -> Unit, showContent: @Composable () -> Unit) {
    //记录下刷新状态
    var isRefreshing by remember {
        mutableStateOf(false)
    }

    val scope = rememberCoroutineScope()   //协成模拟异步操作
    val state = rememberPullRefreshState(isRefreshing, onRefresh = {
        scope.launch {
            isRefreshing = true
            delay(1000)
            onRefreshing()
            isRefreshing = false
        }
    })

    Box(modifier = Modifier
        .fillMaxSize()
        .pullRefresh(state)) {//下拉刷新控件

        showContent()
        //配置默认Indicator,当然也可以自定义
        PullRefreshIndicator(
            refreshing = isRefreshing,
            state = state,
            Modifier.align(Alignment.TopCenter)
        )
    }
}

运行效果:

android compose 下拉刷新

你可能感兴趣的:(compose,android,compose)