我正在尝试学习Jetpack Compose,我的项目中有包含名称和图像的列表项。虽然列表名称没有问题,但是用户图像在模拟器中无法显示。我不知道错过了什么。是否有任何解决方案?
data class Data(
var name: String,
var image: String)
屏幕:
@Composable
fun DataScreen() {
val listOfData = listOf(
Data("name1", "image1"),
Data("name2", "image2"),
Data("name3", "image3"),)
Column(
horizontalAlignment = Alignment.CenterHorizontally,
modifier = Modifier
.fillMaxSize()
.background(Color.White)
) {
LazyColumn(
modifier = Modifier
) {
items(listOfData.size) { index ->
DataListItem(listOfData[index])
}}}}
项目:
@Composable
fun DataListItem(data: Data) {
val context = LocalContext.current
val painter = rememberImagePainter(
data = data.image
)
Column(
modifier = Modifier.padding(5.dp)
) {
Row(
modifier = Modifier
.fillMaxWidth()
.padding(5.dp)
) {
Image(
painter = painter,
contentDescription = null,
contentScale = ContentScale.Crop,
modifier = Modifier
.size(32.dp)
.clip(CircleShape))
Text(
text = data.name,
fontSize = 12.sp,
)}}}