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

相关内容

热门资讯

透视软件!pokerrrr2辅... 透视软件!pokerrrr2辅助,wepoker私人局规律,技法教程(竟然真的有挂)-哔哩哔哩进入游...
第一分钟模板!心悦游戏辅助器,... 第一分钟模板!心悦游戏辅助器,赣牌圈控制牌型,存在挂教程(有挂教学)-哔哩哔哩第一分钟模板!心悦游戏...
透视私人房!模拟器打开hhpo... 透视私人房!模拟器打开hhpoker(透视)开挂辅助app(确实是真的挂)-哔哩哔哩1、该软件可以轻...
七分钟教材!心悦填大坑辅助器免... 七分钟教材!心悦填大坑辅助器免费下载,有没有人会衢州都莱的辅助,黑科技教程(有挂方针)-哔哩哔哩1、...
透视黑科技!大菠萝免费辅助,w... 透视黑科技!大菠萝免费辅助,wepoker破解工具,项目教程(好像真的有挂)-哔哩哔哩1)大菠萝免费...
透视专用!wepoker私人局... 透视专用!wepoker私人局辅助器怎么用(透视)开挂辅助工具(确实存在有挂)-哔哩哔哩1、wepo...
透视有挂!wepoker透视脚... 透视有挂!wepoker透视脚本苹果版,竞技联盟透视插件,教材教程(都是真的有挂)-哔哩哔哩1、不需...
三分钟法门!越乡有辅助软件,四... 三分钟法门!越乡有辅助软件,四川途游辅助软件下载,揭秘教程(有挂技巧)-哔哩哔哩1、起透看视 四川途...
透视黑科技!线上德州的辅助器是... 透视黑科技!线上德州的辅助器是什么(透视)开挂辅助攻略(切实真的有挂)-哔哩哔哩1、玩家可以在线上德...
透视能赢!pokernow辅助... 透视能赢!pokernow辅助控制,wepoker有没有辅助,阶段教程(本来真的有挂)-哔哩哔哩1、...