[英]How inflate composable dynamically in jetpack compose?
I want to inflate composable on trigger events in Jetpack COmpose.我想在 Jetpack COmpose 中的触发事件上扩展可组合项。
If I used XML, I would use Binding.inflate or LayoutInlfater.inflate.如果我使用 XML,我会使用 Binding.inflate 或 LayoutInlfater.inflate。
// example
lifecycleScope.launch {
viewModel.createNumberEffectEvent.collect { number ->
val numberText = CustomAnimationTextBinding.inflate(LayoutInflater.from(context), this, true)
numberText.tvNumber.text = "number: $number"
// View Animation
numberText.slideAnimate()
numberTExt.remove(delay = 3000)
}
}
But I don't know in Jetpack Compose.但我不知道在 Jetpack Compose 中。
I don't think dynamic inflate on compose is a good way because it's declarative programming, but I wonder if there's a way.我不认为 compose 上的动态膨胀是一个好方法,因为它是声明式编程,但我想知道是否有办法。
The code below is an example of what I want.下面的代码是我想要的示例。
LaunchedEffect(Unit) {
viewModel.createNumberEffectEvent.collect { number ->
inflate(CustomAnimationText(text = "create number: $number")) // Like this.
}
}
You should turn your "event" to some state instead because with Jetpack Compose your approach to showing/hiding views is simply checking with the 'if'/'?let' statement your state value and declaring the composable (which will lead to screen recomposition with your composable on it) or not.您应该将您的“事件”改为某个 state,因为使用 Jetpack Compose,您显示/隐藏视图的方法只是简单地检查“if”/“?让”声明您的 state 值并声明可组合(这将导致屏幕重组与你的组合)或不。
In your case I think it can look like this, your composable "screen":在你的情况下,我认为它可以看起来像这样,你的可组合“屏幕”:
val number by viewModel.number.collectAsState<Int?>()
number?.let {
CustomAnimationText(text = "create number: $it")
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.