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

相关内容

热门资讯

第九分钟带你明白!创思维透视下... 第九分钟带你明白!创思维透视下载链接,创思维激k看底牌辅助软件,必备教程(有挂总结)-哔哩哔哩创思维...
六分钟带你发现!哥哥打大a辅助... 【亲,福建天天开心辅助工具 这款游戏可以开挂的,确实是有挂的,很多玩家在这款福建天天开心辅助工具中打...
3分钟带你讲解!乐达大连穷胡小... 大家好,今天小编来为大家解答乐达大连穷胡小鸡满天飞这个问题咨询软件客服可以免费测试直接加微信(136...
四分钟带你透视!789大菠萝攻... 789大菠萝攻略开挂教程视频分享装挂详细步骤在当今的网络游戏中,789大菠萝攻略作为一种经典的娱乐方...
9分钟带你讲解!丽水都莱脚本辅... 9分钟带你讲解!丽水都莱脚本辅助,福建天天开心辅助工具,教你攻略(有挂教程)-哔哩哔哩 >>您好:软...
第4分钟带你明白!福建大玩家透... 第4分钟带你明白!福建大玩家透视,赣牌圈有挂吗,辅助教程(有挂总结)-哔哩哔哩福建大玩家透视ai黑科...
第十分钟带你发现!皮皮跑胡子修... 第十分钟带你发现!皮皮跑胡子修改器,友友联盟免费辅助器,专业教程(有挂总结)-哔哩哔哩1、下载安装好...
一分钟带你科普!叮叮川南游戏辅... 【亲,叮叮川南游戏辅助软件 这款游戏可以开挂的,确实是有挂的,很多玩家在这款叮叮川南游戏辅助软件中打...
8分钟带你讲解!拱趴游戏诀窍,... 8分钟带你讲解!拱趴游戏诀窍,爱来大菠萝辅助iq,扑克教程!(果真有挂)-哔哩哔哩爱来大菠萝辅助iq...
第六分钟带你开挂!免费随意玩辅... 第六分钟带你开挂!免费随意玩辅助器,四川熊猫辅助软件视频,教你攻略(的确有挂)-哔哩哔哩;无需打开直...