前言
习惯了 Android 开发的同学一定知道,UI 的刷新要非常的慎重,尤其是复杂的页面,这会给性能带来一定的损耗。Compose 中有一个非常重要的改念-重组(Recomposition),今天我们就来了解一下重组的规则。
官方文档中有一段对重组范围的说明,大意是如果界面的某些部分无效,Compose 会尽力只重组需要更新的部分。https://developer.android.com/jetpack/compose/mental-model#skips
请大家仔细看下面的例子,思考第一次运行时输出的日志是什么?Button 响应点击事件之后,日志输出又是什么?
@Composable
fun ReCompositionTest() {
var text by remember { mutableStateOf("test") }
Log.d("TAG", "ReCompositionTest: ")
Button(onClick = {
Log.d("TAG", "Button onClick: ")
text = "onClick"
}, content = {
Log.d("TAG", "Button content: ")
Text(text = text)
})
Text1(text)
Text2(text)
Text3()
}
@Composable
fun Text1(text: String) {
Log.d("TAG", "Text1: ")
Text(text)
}
@Composable
fun Text2(text: String){
Log.d("TAG", "Text2: ")
Text("Text2")
}
@Composable
fun Text3(){
Log.d("TAG", "Text3: ")
Text("Text3")
}

最低0.47元/天 解锁文章
697

被折叠的 条评论
为什么被折叠?



