【问题标题】:A change on the state does not recompose the view状态的改变不会重构视图
【发布时间】:2020-01-13 08:32:05
【问题描述】:

我有以下问题,计数器变量的状态更改不会触发 Text 组件的重新组合。关于可能是什么问题的任何想法???

@Model
class CounterState(var counter:Int = 0)

@Composable
fun CounterView(counterState: CounterState){
     Column {
         Text(text= "${counterState.counter}", 
              style=(+typography()).body1)

         Button(text ="Increment", 
                onClick = {counterState.counter++},
                style = ContainedButtonStyle())
    }
}

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContent {
        app {
            CounterView(CounterState())
        }
    }
}

【问题讨论】:

  • 我不确定,但每次点击文本都会改变。
  • 你是在模拟器还是真机上测试? android studio 的预览暂时没有响应点击。

标签: android android-jetpack-compose


【解决方案1】:

var counter by +state { 0 } Column { Text( text = "$counter", style = (+typography()).body1 ) Button( text = "Increment", onClick = { counter++ }, style = ContainedButtonStyle() ) }

【讨论】:

    猜你喜欢
    • 2019-11-15
    • 2019-01-30
    • 2019-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多