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

相关内容

热门资讯

有玩家发现!hhpoker脚本... 有玩家发现!hhpoker脚本,wepoker安装教程(透视)解密教程(有挂详细)-哔哩哔哩1、点击...
透视有挂!wepoker辅助工... 透视有挂!wepoker辅助工具,xpoker怎么辅助(透视)总结教程(证实有挂)-哔哩哔哩1、用户...
透视真的!佛手在线大菠萝技巧,... 透视真的!佛手在线大菠萝技巧,wejoker透视方法(透视)推荐教程(发现有挂)-哔哩哔哩1、该软件...
今天上午!wepoker有透视... 今天上午!wepoker有透视吗,we-poker辅助软件教程(透视)详细教程(有挂教学)-哔哩哔哩...
最新消息!wepoker永久免... 最新消息!wepoker永久免费脚本,wepoker辅助器下载(透视)专业教程(有挂总结)-哔哩哔哩...
此事备受玩家关注!有哪些免费的... 此事备受玩家关注!有哪些免费的wpk辅助码,xpoker怎么辅助(透视)了解教程(有挂方略)-哔哩哔...
此事引发网友热议!wepoke... 此事引发网友热议!wepoker好友助力码,wepoker怎么买辅助(透视)揭幕教程(确实有挂)-哔...
透视总结!哈糖大菠萝软件下载,... 透视总结!哈糖大菠萝软件下载,htx矩阵wepoker辅助(透视)解迷教程(有挂猫腻)-哔哩哔哩该软...
据目击者称!wepoker私人... 据目击者称!wepoker私人局辅助,wejoker私人辅助软件(透视)关于教程(有挂方式)-哔哩哔...
网友热议!拱趴大菠萝辅助方法,... 网友热议!拱趴大菠萝辅助方法,wepoker有没有插件(透视)揭幕教程(揭秘有挂)-哔哩哔哩1、打开...