表达式中 ObservableFields 的 get()
get() on ObservableFields in an expression
我有一个相当简单的数据绑定布局:
<data>
<variable
name="viewModel"
type="MyViewModel" />
<variable
name="navigator"
type="MyNavigator" />
</data>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:onClick="@{() -> navigator.goto(viewModel.insurance, viewModel.title)}"
android:text="Click">
这是 Kotlin 中的视图模型:
class MyViewModel {
val title = ObservableInt()
val insurance = ObservableField<Insurance?>()
}
这是 Kotlin 中的导航器:
class MyNavigator(private val activity: MyActivity) {
fun goto(insurance: Insurance?, @StringRes title: Int) {
if (insurance != null) {
val intent = OtherActivity.newIntent(activity, insurance,title)
activity.startActivity(intent)
} else {
Timber.w("goToClaimsQuestionsList but no insurance")
}
}
}
预期:
当我单击该按钮时,Navigator 应该会收到该事件,并启动另一个 activity。 但是,insurance Insurance?
始终为 null。
当我在布局中使用这个表达式时(使用.get()
):
android:onClick="@{() -> navigator.goto(viewModel.insurance.get(), viewModel.title.get())}"
一切正常,但我在构建时收到警告:
Warning:warning: Do not explicitly call 'get()' on ObservasbleFields in an expression. This support will be removed soon. 'viewModel.insurance.get()'
这是当前 Kotlin 数据绑定实现中的错误吗?或者是否有另一种解释为什么我必须使用 ObservableField.get()
?
注意:
- 适用于 Mac
的 AndroidStudio 3.0
- 构建工具版本:27.0.3
- android gradle 插件: 3.0.1
- 科特林版本:1.2.21
它似乎是 bug 与 Kotlin 无关的。
但在你的情况下,有人可能会争辩说,最好不要从 XML 层使用它。
相反,您可以创建一个带有回调的变量来封装导航逻辑
class Callbacks(val vm: MyViewModel, val navigator: MyNavigator) {
fun buttonClicked() = navigator.goto(vm.insurance.get(), vm.title.get())
}
并调用这个函数onClick
android:onClick="@{() -> callbacks.buttonClicked())}"
我有一个相当简单的数据绑定布局:
<data>
<variable
name="viewModel"
type="MyViewModel" />
<variable
name="navigator"
type="MyNavigator" />
</data>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:onClick="@{() -> navigator.goto(viewModel.insurance, viewModel.title)}"
android:text="Click">
这是 Kotlin 中的视图模型:
class MyViewModel {
val title = ObservableInt()
val insurance = ObservableField<Insurance?>()
}
这是 Kotlin 中的导航器:
class MyNavigator(private val activity: MyActivity) {
fun goto(insurance: Insurance?, @StringRes title: Int) {
if (insurance != null) {
val intent = OtherActivity.newIntent(activity, insurance,title)
activity.startActivity(intent)
} else {
Timber.w("goToClaimsQuestionsList but no insurance")
}
}
}
预期:
当我单击该按钮时,Navigator 应该会收到该事件,并启动另一个 activity。 但是,insurance Insurance?
始终为 null。
当我在布局中使用这个表达式时(使用.get()
):
android:onClick="@{() -> navigator.goto(viewModel.insurance.get(), viewModel.title.get())}"
一切正常,但我在构建时收到警告:
Warning:warning: Do not explicitly call 'get()' on ObservasbleFields in an expression. This support will be removed soon. 'viewModel.insurance.get()'
这是当前 Kotlin 数据绑定实现中的错误吗?或者是否有另一种解释为什么我必须使用 ObservableField.get()
?
注意:
- 适用于 Mac 的 AndroidStudio 3.0
- 构建工具版本:27.0.3
- android gradle 插件: 3.0.1
- 科特林版本:1.2.21
它似乎是 bug 与 Kotlin 无关的。
但在你的情况下,有人可能会争辩说,最好不要从 XML 层使用它。
相反,您可以创建一个带有回调的变量来封装导航逻辑
class Callbacks(val vm: MyViewModel, val navigator: MyNavigator) {
fun buttonClicked() = navigator.goto(vm.insurance.get(), vm.title.get())
}
并调用这个函数onClick
android:onClick="@{() -> callbacks.buttonClicked())}"