每个行添加点击事件,可以使用`Clickable`组件。在`Clickable`组件的`onClick`参数中,您可以指定要在用户单击行时执行的操作。下面是一个示例代码:
@Composable
fun ImageTextList(imageTextList: List<ImageTextItem>, onItemClick: (ImageTextItem) -> Unit) {
LazyColumn {
itemsIndexed(imageTextList) { index, item ->
ImageTextListItem(index, item, onItemClick)
}
}
}
@Composable
fun ImageTextListItem(index: Int, item: ImageTextItem, onItemClick: (ImageTextItem) -> Unit) {
Clickable(onClick = { onItemClick(item) }) {
Row(
modifier = Modifier
.fillMaxWidth()
.padding(16.dp)
) {
Image(
painter = painterResource(id = item.imageId),
contentDescription = null,
modifier = Modifier
.size(64.dp)
.clip(shape = CircleShape)
)
Spacer(modifier = Modifier.width(16.dp))
Text(
text = item.title,
style = MaterialTheme.typography.h6,
modifier = Modifier.weight(1f)
)
Text(
text = item.subtitle,
style = MaterialTheme.typography.body2,
modifier = Modifier.align(Alignment.CenterVertically)
)
}
}
}
data class ImageTextItem(
val imageId: Int,
val title: String,
val subtitle: String
)
val imageTextList = listOf(
ImageTextItem(
imageId = R.drawable.image1,
title = "Title 1",
subtitle = "Subtitle 1"
),
ImageTextItem(
imageId = R.drawable.image2,
title = "Title 2",
subtitle = "Subtitle 2"
),
ImageTextItem(
imageId = R.drawable.image3,
title = "Title 3",
subtitle = "Subtitle 3"
),
)
@Preview
@Composable
fun PreviewImageTextList() {
ImageTextList(imageTextList = imageTextList, onItemClick = { item ->
// Handle item click event
})
}
最后:推荐一款基于openai引擎的idea中ai生成代码的插件,使用插件可以很方便的询问查找生成想要的代码,Idea上的Ai生成代码插件