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

相关内容

热门资讯

关于透视!哈糖大菠萝可以开挂吗... 关于透视!哈糖大菠萝可以开挂吗!切实一直都是有辅助app(详细教程)-哔哩哔哩1、许多玩家不知道哈糖...
科普透视!佛手大菠萝辅助!总是... 科普透视!佛手大菠萝辅助!总是是有辅助软件(有挂功能)-哔哩哔哩1、下载好佛手大菠萝辅助脚本下载之后...
揭幕透视!竞技联盟透视!竟然是... 揭幕透视!竞技联盟透视!竟然是有辅助脚本(有挂透明挂)-哔哩哔哩1、实时竞技联盟透视透视辅助更新:用...
分享透视!佛手在线有挂吗!真是... 分享透视!佛手在线有挂吗!真是是真的有辅助app(有挂辅助)-哔哩哔哩;1、每一步都需要思考,不同水...
总结透视!大菠萝免费辅助!一直... 总结透视!大菠萝免费辅助!一直是真的有辅助方法(确实有挂)-哔哩哔哩;1、下载好大菠萝免费辅助脚本下...
开挂透视!拱趴大菠萝有什么挂!... 开挂透视!拱趴大菠萝有什么挂!竟然真的有辅助脚本(有挂方略)-哔哩哔哩1)拱趴大菠萝有什么挂免费钻石...
了解透视!约局吧能不能开挂!真... 了解透视!约局吧能不能开挂!真是真的是有辅助app(有挂分析)-哔哩哔哩1、全新机制【约局吧能不能开...
解迷透视!德州透视是真的吗!一... 解迷透视!德州透视是真的吗!一直是真的有辅助插件(讲解有挂)-哔哩哔哩德州透视是真的吗透视方法中分为...
分享透视!xpoker辅助!本... 分享透视!xpoker辅助!本来是真的有辅助神器(有挂规律)-哔哩哔哩xpoker辅助是不是有人用挂...
推荐透视!xpoker辅助!其... 推荐透视!xpoker辅助!其实有辅助软件(有挂详细)-哔哩哔哩1、操作简单,无需xpoker辅助手...