Mat*_*att 2 android kotlin android-databinding android-livedata
有没有办法用 LiveData 和数据绑定来做这样的事情?
ViewModel 有这个属性:
val weather: LiveData<UnitSpecificCurrentWeatherEntry>
Run Code Online (Sandbox Code Playgroud)
我想在布局中做什么:
<TextView
android:id="@+id/textView"
android:text="@{viewmodel.weather.value.someProperty}"... />
Run Code Online (Sandbox Code Playgroud)
这是否可以以任何方式实现,或者我是否必须针对所包含对象的每个属性将 LiveData 中包含的对象拆分为多个对象?
从 MVVM 模式的角度来看,这并不完全正确。在您的示例视图中,需要了解显示数据的属性路径。最好直接从 提供目标数据ViewModel
。如果您的财产依赖于另一个财产,您可以使用Transformations
:
val weather: LiveData<UnitSpecificCurrentWeatherEntry> = //suppose, we have instantiation here
val someProperty: LiveData<SomePropertyType> = Transformations.map(weather) { it.someProperty }
Run Code Online (Sandbox Code Playgroud)
现在,您可以在 xml 中使用它:
<TextView
android:id="@+id/textView"
android:text="@{viewmodel.someProperty}"/>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
810 次 |
最近记录: |