重组日志打印那里实际有点不一样,为什么?
来源:11-7 Button组件详解(二)

慕UI8482615
2024-08-23
这个例子有两个remember修饰的变量,如果button设置了interactionSource,那么点击时发生重组所以Log能打印这个理解,但如果button不用interactionSource(也就是注释掉),单独改变cout变量,点击时缺没有触发Log打印,都是重组但为什么实际效果有区别?
@Preview
@Composable
fun ButtonPreview() {
val count = remember {
mutableIntStateOf(0)
}
val interaction = remember {
MutableInteractionSource()
}
val state = when {
interaction.collectIsPressedAsState().value -> {
ButtonState("点击中", Color.Red, Color.Black)
}
else -> {
ButtonState("放手了", Color.White, Color.Green)
}
}
Log.d("tag","1")
Button(
onClick = {
count.intValue++
},
interactionSource = interaction,
colors = ButtonDefaults.buttonColors(containerColor = state.buttonColor, contentColor = state.textColor)
) {
Text(text = "${state.text}${count.intValue}", )
}
}
然后这个例子缺又是可以正常打印,我感觉是基本数据类型和引用数据类型的重组逻辑不一样
@Preview
@Composable
fun TextFieldPreview() {
var text by remember {
mutableStateOf("")
}
Log.d("tag","1")
TextField(
value = text,
onValueChange = {
text = it
},
label = {
},
)
}
写回答
1回答
-
慕UI8482615
提问者
2024-08-23
貌似也不是数据类型的原因,试过把例子1里修改int改成修改引用数据类型也一样,不知道是什么情况
012024-08-23
相似问题