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

相关内容

热门资讯

透视脚本!wepoker辅助透... 透视脚本!wepoker辅助透视软件,hhpoker软件靠谱吗,新版2025教程(往昔真的有挂);超...
透视有挂!智星德州菠萝透视插件... 透视有挂!智星德州菠萝透视插件工具,wepoker有透视吗,AI教程(固有真的是有挂);(需添加指定...
透视科技!wepoker辅助器... 透视科技!wepoker辅助器怎么用,aapoker怎么提高中牌率,必备教程(原生真的有挂)1、完成...
透视能赢!aapoker免费透... 透视能赢!aapoker免费透视脚本,hhpoker软件靠谱吗,透牌教程(果然存在有挂)aapoke...
透视私人局!wpk脚本是什么,... 透视私人局!wpk脚本是什么,德州透视脚本,德州教程(切实真的是有挂);是一款可以让一直输的玩家,快...
透视神器!wepoekr底牌透... 透视神器!wepoekr底牌透视,werplan外卦神器,透明挂教程(竟然真的是有挂);wepoek...
透视系统!aapoker辅助包... 透视系统!aapoker辅助包,wpk辅助器,我来教教你(切实有挂);aapoker辅助包软件透明挂...
透视工具!wpk透视脚本下载,... 1、透视工具!wpk透视脚本下载,wepoker辅助脚本,透明挂教程(真是有挂)。2、wpk透视脚本...
透视模拟器!wepoker辅助... 1、透视模拟器!wepoker辅助下载,werplan外卦神器,安装教程(固有真的有挂);详细教程。...
透视app!wepoker免费... 这是一款非常优秀的wepoker免费透视脚本 ia辅助检测软件,能够让你了解到wepoker免费透视...