AsyncImage在LazyColumnKotlinCompose中的使用
创始人
2024-09-21 04:30:52
0
  1. 导入必要的Compose库 implementation "androidx.compose.foundation:foundation:1.0.0-rc02"implementation "androidx.compose.material:material:1.0.0-rc02"

  2. 在需要使用AsyncImage的地方,引入 androidx.compose.foundation.lazy.LazyColumn,例如:

LazyColumn {
    itemsIndexed(images) { _, imageUrl ->
        AsyncImage(
            modifier = Modifier.fillMaxWidth(),
            contentScale = ContentScale.Crop,
            content = {
                CoilImage(
                    data = imageUrl,
                    contentDescription = null,
                    contentScale = ContentScale.Crop,
                    fadeIn = true,
                    loading = {
                        //显示加载中
                        Box(
                            Modifier
                                .fillMaxSize()
                                .background(Color.LightGray)
                        ) {
                            CircularProgressIndicator(Modifier.align(Alignment.Center))
                        }
                    }
                )
            }
        )
    }
}

这里使用了Coil作为图片加载库。在加载图片时,AsyncImage会显示加载中的进度条。

注意:由于还处于Composable函数编写的过程中,记得使用@Preview注解提供预览效果。

@Preview(showBackground = true)
@Composable
fun PreviewAsyncImage() {
    MyTheme {
        LazyColumn {
            itemsIndexed(images) { _, imageUrl ->
                AsyncImage(
                    modifier = Modifier.fillMaxWidth(),
                    contentScale = ContentScale.Crop,
                    content = {
                        CoilImage(
                            data = imageUrl,
                            contentDescription = null,
                            contentScale = ContentScale.Crop,
                            fadeIn = true,
                            loading = {
                                //显示加载中
                                Box(
                                    Modifier
                                        .fillMaxSize()
                                        .background(Color.LightGray)
                                ) {
                                    CircularProgressIndicator(Modifier.align(Alignment.Center))
                                }
                            }
                        )
                    }
                )
            }
        }
    }
}

其中MyTheme提供了样式和颜色,可以在Theme.kt文件中定义。

@Composable
fun MyTheme(content: @Composable () -> Unit) {
    MaterialTheme(
        colors = Colors,
        typography = Typography,
        content = content

相关内容

热门资讯

透视好牌!红龙poker透视,... 透视好牌!红龙poker透视,wepoker插件功能辅助器(详细辅助辅助教程)1、红龙poker透视...
微扑克辅助机器人!wepoke... 微扑克辅助机器人!wepoke管理员(透视)切实存在有挂(详细辅助黑科技教程);微扑克辅助机器人!w...
透视神器!智星德州菠萝插件官网... 透视神器!智星德州菠萝插件官网,拱趴大菠萝万能辅助器(详细辅助技巧教程)一、智星德州菠萝插件官网软件...
wepower有外挂!微扑克的... wepower有外挂!微扑克的辅助工具(透视)从前是有挂(详细辅助解密教程)1、金币登录送、破产送、...
透视辅助!wepoker透视脚... 透视辅助!wepoker透视脚本下载,aapoker怎么设置抽水(详细辅助分享教程)1、完成wepo...
wepoke有没有挂!wepo... wepoke有没有挂!wepoke最新下载地址(透视)切实存在有挂(详细辅助力荐教程)wepoke有...
透视中牌率!wpk辅助购买,w... 透视中牌率!wpk辅助购买,wepoker辅助器怎么弄(详细辅助扑克教程);1、实时wpk辅助购买开...
wepoke真的有挂!微扑克必... wepoke真的有挂!微扑克必胜技巧(透视)一贯是有挂(详细辅助技巧教程),您好,wepoke真的有...
透视安装!aapoker辅助挂... 透视安装!aapoker辅助挂,wepoker插件程序激活码(详细辅助软件教程)1、每一步都需要思考...
微扑克辅助机器人!wpk透明挂... 微扑克辅助机器人!wpk透明挂(透视)原来真的是有挂(详细辅助解密教程)1、许多玩家不知道微扑克辅助...