如果它是 jetpack compose 中类的实例,如何通知状态更改
我有这样的代码:
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
TestsTheme {
var myvalue = remember{ mutableStateOf(4)}
Row() {
CustomText(myvalue = myvalue)
CustomButton(myvalue = myvalue)
}
}
}
}
}
@Composable
fun CustomButton(myvalue: MutableState<Int>) {
Button(onClick = {myvalue.value++}) {
}
}
@Composable
fun CustomText(myvalue: MutableState<Int>) {
Text("${myvalue.value}")
}
当我单击 CustomButton 时,CustomText 重新组合并更改它的值(而且没问题),但是如果我用某个类的对象替换 myvalue ,它就不起作用
如果我如何使 CustomText 重新组合myvalue 现在是 TestClass 的一个实例,并且它在下面的代码中发生了变化?
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
TestsTheme {
var myvalue = remember{ mutableStateOf(TestClass(4))}
Row() {
CustomText(myvalue = myvalue)
CustomButton(myvalue = myvalue)
}
}
}
}
}
@Composable
fun CustomButton(myvalue: MutableState<TestClass>) {
Button(onClick = {myvalue.value.a++}) {
}
}
@Composable
fun CustomText(myvalue: MutableState<TestClass>) {
Text("${myvalue.value.a}")
}
data class TestClass(var a: Int) {
}
I've got code like this:
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
TestsTheme {
var myvalue = remember{ mutableStateOf(4)}
Row() {
CustomText(myvalue = myvalue)
CustomButton(myvalue = myvalue)
}
}
}
}
}
@Composable
fun CustomButton(myvalue: MutableState<Int>) {
Button(onClick = {myvalue.value++}) {
}
}
@Composable
fun CustomText(myvalue: MutableState<Int>) {
Text("${myvalue.value}")
}
When I click CustomButton, CustomText recomposes and changes it's value (and it is ok), but if I replace myvalue with an object of some class it does'nt work
How do I make the CustomText recompose if myvalue is now an instance of TestClass and it changes in the code below?
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
TestsTheme {
var myvalue = remember{ mutableStateOf(TestClass(4))}
Row() {
CustomText(myvalue = myvalue)
CustomButton(myvalue = myvalue)
}
}
}
}
}
@Composable
fun CustomButton(myvalue: MutableState<TestClass>) {
Button(onClick = {myvalue.value.a++}) {
}
}
@Composable
fun CustomText(myvalue: MutableState<TestClass>) {
Text("${myvalue.value.a}")
}
data class TestClass(var a: Int) {
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Compose 只能理解,如果您设置了 State 对象的值,它应该重新组合。在您的示例中,
myvalue.value++
实际上只是myvalue.value = myvalue.value.inc()
的缩写,因此触发重组的是调用状态。值设置器。
如果您只是改变状态所持有的对象的属性,则不会调用 setter 并且不会发生重组。
因此,您有两个选择来解决此问题:
TestClass
不可变并使用新对象更新State
这不太好读,但不需要
TestClass
包含任何特定于 compose 的代码。TestClass
成为状态持有者Compose can only understand that it should recompose if you set the value of a
State
object. In your examplemyvalue.value++
is really just short formyvalue.value = myvalue.value.inc()
, so what triggers the recomposition is calling thestate.value
setter.If you just mutate a property of the object held by a state, you are not calling the setter and no recomposition happens.
So you have 2 options to solve this:
TestClass
immutable and update theState
with a new objectThis is not really nice to read but doesn't require
TestClass
to contain any compose-specific code.TestClass
a state holder