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

相关内容

热门资讯

6分钟学习!雀友会广东潮汕辅助... 6分钟学习!雀友会广东潮汕辅助,hhpoker免费辅助器,攻略教程(有挂方式)-哔哩哔哩运雀友会广东...
四分钟绝活儿!新道游正版辅助,... 四分钟绝活儿!新道游正版辅助,卡农辅助,2025版教程(有挂教程)-哔哩哔哩该软件可以轻松地帮助玩家...
2分钟诀窍!微友辅助器免费版v... 2分钟诀窍!微友辅助器免费版v3.0,川友汇挂牌,透牌教程(有挂教学)-哔哩哔哩1、微友辅助器免费版...
3分钟讲义!甘肃白银麻将辅助软... 3分钟讲义!甘肃白银麻将辅助软件,微信边锋辅助软件,靠谱教程(的确有挂)-哔哩哔哩1、玩家可以在甘肃...
三分钟诀窍!福建兄弟十三水辅助... 三分钟诀窍!福建兄弟十三水辅助器下载,欢乐茶馆辅助,揭秘教程(存在有挂)-哔哩哔哩一、福建兄弟十三水...
6分钟资料!摸一把游戏辅助软件... 6分钟资料!摸一把游戏辅助软件,小闲辅助神器,解密教程(真实有挂)-哔哩哔哩1、进入到摸一把游戏辅助...
第9分钟讲义!人海大厅辅助插件... 第9分钟讲义!人海大厅辅助插件,拱趴大菠萝修改器,爆料教程(有挂教学)-哔哩哔哩人海大厅辅助插件辅助...
3分钟教程书!新众乐辅助,情怀... 3分钟教程书!新众乐辅助,情怀娱乐辅助器,德州论坛(有挂存在)-哔哩哔哩1、首先打开情怀娱乐辅助器最...
十分钟资料!白银胡乐白银版辅助... 十分钟资料!白银胡乐白银版辅助,小程序边锋辅助,普及教程(有挂工具)-哔哩哔哩1、白银胡乐白银版辅助...
第四分钟策略!途游手游辅助脚本... 第四分钟策略!途游手游辅助脚本平台,海盗来了辅助器最新,wpk教程(有挂实锤)-哔哩哔哩1、每一步都...