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

相关内容

热门资讯

辅助开挂!青鸟辅助安卓,约局吧... 辅助开挂!青鸟辅助安卓,约局吧辅助器(透视)开挂辅助脚本(有挂工具);青鸟辅助安卓是一项青鸟辅助安卓...
开挂辅助!科乐辅助工作室,we... 开挂辅助!科乐辅助工作室,wepoker私人局开挂视频(透视)开挂辅助工具(有挂神器);科乐辅助工作...
开挂辅助!天天互娱app辅助,... 开挂辅助!天天互娱app辅助,拱趴游戏破解器(透视)开挂辅助插件(存在有挂);详细天天互娱app辅助...
开挂辅助!大众互娱脚本,wep... 开挂辅助!大众互娱脚本,wepoker是不是有人用挂(透视)开挂辅助平台(有挂方略);一、大众互娱脚...
开挂辅助!蜀山手游全自动辅助,... 开挂辅助!蜀山手游全自动辅助,wepoker怎么提高运气(透视)开挂辅助脚本(今日头条);亲真的是有...
辅助开挂!龙岩闲游安卓脚本,w... 辅助开挂!龙岩闲游安卓脚本,wepoker轻量版有透视吗(透视)开挂辅助神器(有挂方式);大家肯定在...
开挂辅助!哥哥打大a游戏攻略,... 开挂辅助!哥哥打大a游戏攻略,wepoker私人局有透视吗(透视)开挂辅助插件(果真有挂);哥哥打大...
开挂辅助!h5大厅反杀,德扑H... 【福星临门,好运相随】;开挂辅助!h5大厅反杀,德扑HHpoker有挂吗(透视)开挂辅助神器(有挂教...
开挂辅助!微信小程序微乐游戏辅... 开挂辅助!微信小程序微乐游戏辅助,德普之星透视(透视)开挂辅助神器(有人有挂);微信小程序微乐游戏辅...
辅助开挂!杭州边锋辅助软件,w... 辅助开挂!杭州边锋辅助软件,wpk辅助插件(透视)开挂辅助脚本(发现有挂);是一款可以让一直输的玩家...