brabo-hi
02/21/2022, 1:54 AMRow {
AndroidView(factory = { context -> CustomView(context, "A") }, update = {})
AndroidView(factory = { context -> CustomView(context, "B") }, update = {})
Button(onClick = { swapViews() }) {
Text(text = "Swap view")
}
}
swapViews()
should swap both views. but it looks like i have to manually remove each view from parent. Should i call removeView()
in each update{}
? here is the error i am getting
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.`
Adam Powell
02/21/2022, 2:25 AMkey
composablesAdam Powell
02/21/2022, 2:27 AMAdam Powell
02/21/2022, 2:28 AMAdam Powell
02/21/2022, 2:28 AMAndroidView
will do the restbrabo-hi
02/21/2022, 2:29 AMkey
 composablesAdam Powell
02/21/2022, 2:31 AMval items = remember { mutableStateListOf("A", "B") }
items.forEach { item ->
key(item) {
AndroidView(factory = { context -> CustomView(context, item) }, update = {})
}
}
Adam Powell
02/21/2022, 2:31 AMitems
to change the order the views appear inbrabo-hi
02/21/2022, 3:20 AMvar toggle by remember{ mutableStateOf(true)}
Column {
if (toggle) {
AndroidView(factory = { context -> CustomView(context, "A") }, update = {})
Divider()
AndroidView(factory = { context -> CustomView(context, "B") }, update = {})
} else {
AndroidView(factory = { context -> CustomView(context, "B") }, update = {})
Divider()
AndroidView(factory = { context -> CustomView(context, "A") }, update = {})
}
}
Button(onClick = { toggle = !toggle }) {
Text(text = "Swap view")
}
i put Divider()
just to say that i am many views between.
The first time toggle == true
if the A
…`B`
The next time toggle == false
it tries to replace A
with B
but it finds that B
hasn’t been completely removed. that’s where i am having issuebrabo-hi
02/21/2022, 3:23 AMA
and B
are displayed at the same time but at different position depending on the toogleStatebrabo-hi
02/21/2022, 10:19 PMAdam Powell
02/21/2022, 10:46 PMCustomView(context, "A")
not a constructor call here? I would expect the code above to create two new views and remove the old two views, abandoning them to the garbage collector each time toggle
switches valuebrabo-hi
02/22/2022, 12:06 AMViewB
before removing it. Which technically placed the same view at two places at the same timesbrabo-hi
02/22/2022, 12:08 AMthis is where the issue is since ViewB is still at position Y
• remove ViewB at position Y
• place viewA at position YAdam Powell
02/22/2022, 12:39 AMAdam Powell
02/22/2022, 12:39 AMColumn {
var toggle by remember { mutableStateOf(false) }
if (toggle) {
AndroidView(
{ View(it).apply { setBackgroundColor(0xff00ff00.toInt()); println("created $this") } },
Modifier.size(48.dp)
)
AndroidView(
{ View(it).apply { setBackgroundColor(0xffff0000.toInt()); println("created $this") } },
Modifier.size(48.dp)
)
} else {
AndroidView(
{ View(it).apply { setBackgroundColor(0xffff0000.toInt()); println("created $this") } },
Modifier.size(48.dp)
)
AndroidView(
{ View(it).apply { setBackgroundColor(0xff00ff00.toInt()); println("created $this") } },
Modifier.size(48.dp)
)
}
Button(onClick = { toggle = !toggle }) {
Text("Switch")
}
}
Adam Powell
02/22/2022, 12:45 AMAdam Powell
02/22/2022, 12:49 AMval list = remember { mutableStateListOf(0xff00ff00.toInt(), 0xffff0000.toInt()) }
list.forEach { item ->
key(item) {
AndroidView(
{ View(it).apply { setBackgroundColor(item); println("created $this") } },
Modifier.size(48.dp)
)
}
}
Button(onClick = { list.reverse() }) {
Text("Switch")
}
Adam Powell
02/22/2022, 12:55 AM