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

相关内容

热门资讯

透视代打!aapoker辅助器... 透视代打!aapoker辅助器是真的吗(透视)丫丫陕西扑克绿色透视(本来是真的辅助软件)-哔哩哔哩1...
透视软件!wepoker破解版... 透视软件!wepoker破解版内购(透视)兴动互娱游戏辅助器好用吗(果然真的有辅助软件)-哔哩哔哩1...
据了解!wpk刷入池率脚本(透... 据了解!wpk刷入池率脚本(透视)新蜜瓜大厅免费控制器(其实存在有辅助app)-哔哩哔哩1、任何新蜜...
今日!aapoker透视怎么用... 今日!aapoker透视怎么用(透视)传送屋有挂吗(真是是真的辅助工具)-哔哩哔哩1、传送屋有挂吗免...
透视辅助!wepoker科技辅... 透视辅助!wepoker科技辅助器(透视)wepoker辅助器是真的吗(切实是真的辅助下载)-哔哩哔...
透视真的!wepoker透视辅... 透视真的!wepoker透视辅助下载(透视)天酷辅助器(真是真的有辅助神器)-哔哩哔哩1、下载好天酷...
最终!wepoker国外版透视... 最终!wepoker国外版透视(透视)hhpoker大菠萝有没有作比(本来真的是有辅助安装)-哔哩哔...
透视线上!wepoker辅助工... 透视线上!wepoker辅助工具(透视)余干中至小程序辅助器免费(果然有辅助软件)-哔哩哔哩该软件可...
透视真的!wpk官网下载链接(... 透视真的!wpk官网下载链接(透视)卡农大厅辅助(好像真的是有辅助app)-哔哩哔哩小薇(辅助器软件...
近日!WePoKer辅助器(透... 近日!WePoKer辅助器(透视)悠闲游戏辅助(总是真的有辅助神器)-哔哩哔哩1、玩家可以在悠闲游戏...