【发布时间】:2020-10-03 21:46:11
【问题描述】:
我有一个应用程序,它显示了封装在AndroidView 中的几个不同视图。在下面要重现的简单示例中,这些只是 TextView 实例。问题是更改文本(在这种情况下循环三个不同的值)似乎不会更新应用程序显示的内容。
sealed class AppView
data class ShowSomeText(val text: String) : AppView()
data class SomeOtherState(val data: Any?) : AppView()
data class ShowSomeText2(val text: String) : AppView()
class AppViewModel : ViewModel() {
var currentView = MutableLiveData<AppView>(ShowSomeText("original text"))
var currentViewWorkaround = MutableLiveData<AppView>(ShowSomeText("original text"))
private val textRing = arrayOf("one", "two", "three")
private var textRingPosition = 0
fun incrementTextState() {
val nextState = ShowSomeText(textRing[textRingPosition])
currentView.postValue(nextState)
val nextStateWorkaround = when(currentViewWorkaround.value) {
is ShowSomeText -> ShowSomeText2(textRing[textRingPosition])
else -> ShowSomeText(textRing[textRingPosition])
}
currentViewWorkaround.postValue(nextStateWorkaround)
textRingPosition = (textRingPosition + 1) % textRing.size
}
}
class MainActivity : AppCompatActivity() {
private val viewModel = AppViewModel()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
ViewContainer(viewModel)
}
}
}
@Composable
fun ViewContainer(viewModel: AppViewModel) {
// Add this to gradle.build for the observeAsState function:
// implementation "androidx.compose.runtime:runtime-livedata:$compose_version"
val currentView: AppView by viewModel.currentView.observeAsState(ShowSomeText("starting text"))
val currentViewWorkaround: AppView by viewModel.currentViewWorkaround.observeAsState(ShowSomeText("starting text"))
Column {
Button(onClick = viewModel::incrementTextState) {
Text(
text = "tap to change",
style = TextStyle(fontSize = 12.em)
)
}
Text("Compose Text")
when (currentView) {
is ShowSomeText -> createComposeTextView((currentView as ShowSomeText).text)
is SomeOtherState -> Text("the other state")
}
Text("AndroidView wrapping TextView")
when (currentView) {
is ShowSomeText -> createAndroidViewForTextView((currentView as ShowSomeText).text)
is SomeOtherState -> Text("the other state")
}
Text("AndroidView wrapping TextView with 2-state workaround")
when (currentViewWorkaround) {
is ShowSomeText -> createAndroidViewForTextView((currentViewWorkaround as ShowSomeText).text)
is ShowSomeText2 -> createAndroidViewForTextView((currentViewWorkaround as ShowSomeText2).text)
is SomeOtherState -> Text("the other state")
}
}
}
@Composable
fun createAndroidViewForTextView(text: String) {
val context = ContextAmbient.current
val tv = remember(text, context) {
val x = TextView(context)
x.text = text
x.textSize = 48.0f
x
}
AndroidView({ tv })
}
@Composable
fun createComposeTextView(text: String) {
Text(text, style = TextStyle(fontSize = 12.em))
}
第一个文本通过 Compose Text 函数显示并且可以工作,第二个使用 TextView 包装了一个 AndroidView Compose 函数并且不起作用,第三个也使用相同的 AndroidView 包装器,但通过使用另一个状态变量以某种方式触发了更改。
为什么中间的文字没有更新?
带有 hack 修复的复制 kt 文件的完整要点:https://gist.github.com/okhobb/ba7791af4562ea672d0c52769a7cd8ba
============
更新:基于接受的答案的工作代码:
@Composable
fun TraditionalViewAsComposable(text: String){
var updatableString by remember{mutableStateOf("")}
updatableString = text
AndroidView(
factory={ TextView(it).apply {
this.text = text
this.textSize = 48.0f
} },
update={ it.text = updatableString }
)
}
【问题讨论】:
-
我不确定您要达到什么目标 - 但我看到的第一点是您的 CircularList 无法按您的意愿工作。
textRingPosition会改变它的值,但你不会听/观察变化。您需要告诉 Compose 这个值可能会改变,为此,您需要一个状态val textRingPosition = remember{ mutableStateOf<Int>(0) }。 -
我将状态包装在记忆中,因为我想在重组后保留状态。也许这些 defs 有助于记住这样做:'记住计算产生的值。计算只会在作文期间进行评估。重新组合将始终返回组合产生的值。 State: 'MutableState 类是一个单一的值持有者,其读写被 Compose 观察。此外,对它的写入作为快照系统的一部分进行处理。在组合过程中,您可能希望使用 state 和 stateFor 组合而不是这个工厂函数。'
-
为了可读性:您可以使用 em 扩展功能,例如
12.em,而不是TextUnit.Companion.Em(12) -
嗯,我认为正在观看的内容是
val currentView: PostplaneAppView by viewModel.currentView.observeAsState(ShowSomeText("starting text")),如果我进行建议的两个更改中的任何一个,它确实可以按预期工作。 -
花了很长时间才弄明白,但原因正如我所说,但一开始并不那么明显。查看答案:stackoverflow.com/a/64189703/9957384
标签: android kotlin android-view android-jetpack-compose