数据绑定和包含的布局:找不到onClick的setter属性 [英] Databinding and included layouts: Cannot find setter attribute for onClick
问题描述
我正在尝试为<include>
d布局设置OnClickListener
,但是在编译时收到数据绑定错误,指出数据绑定无法找到参数类型为android的属性'android:onClick'的设置方法.view.View.OnClickListener".
I'm trying to set an OnClickListener
for an <include>
d layout, but receive a data binding error at compile time stating that data binding "Cannot find the setter for attribute 'android:onClick' with parameter type android.view.View.OnClickListener".
这里的上下文是我正在使用数据绑定来使包含的布局膨胀,以便可以将已经绑定到包含布局的viewModel
中的值传递给它.
Context here is that I'm using data binding to inflate the included layout, so that I can pass values into it from a viewModel
that I've bound to the including layout.
我为数据绑定表达式尝试了各种语法:
I've tried various syntax for the data binding expression:
-
@{viewModel::onClickFunction}
-
@{viewModel.onClickFunction}
-
@{() -> viewModel.onClickFunction()}
-
@{(view) -> viewModel.onClickFunction()}
-
@{_ -> viewModel.onClickFunction()}
@{viewModel::onClickFunction}
@{viewModel.onClickFunction}
@{() -> viewModel.onClickFunction()}
@{(view) -> viewModel.onClickFunction()}
@{_ -> viewModel.onClickFunction()}
我已经尝试使用onClickFunction
作为函数以及作为OnClickListener
对象的上述所有内容.
I've tried all of the above with onClickFunction
as a function, and also as an OnClickListener
object.
Stack Overflow上的其他相关问题似乎可以通过清理项目以重新生成数据绑定文件来解决此问题,但这对我没有用.
Other related questions on Stack Overflow seem to solve this issue by cleaning the project to regenerate the databinding files, but that hasn't worked for me.
以下相关代码段:
class MyViewModel() {
val onClickFunctionAsAListener: OnClickListener = object:OnClickListener{
override fun onClick(v: View?) {
//Do something
}
}
fun onClickFunction() {
//Do something
}
}
包括布局
<layout>
<data>
<variable name="viewModel" type="full.package.name.MyViewModel"/>
</data>
<LinearLayout>
<include
layout="@layout/included_layout"
android:onClick="@{viewModel.onClickListener}"
app:customAttribute="@{`someText`}/>
</LinearLayout>
</layout>
包含的布局
<layout>
<data>
<variable name="customAttribute" type="String"/>
</data>
<TextView
layout="@layout/included_layout"
android:text="@{customAttribute}"/>
</layout>
推荐答案
似乎您实际上不能直接将OnClick处理程序直接分配给<include>
标记.通过在IncludedLayout
的数据绑定类中添加另一个变量,然后将OnClickListener分配给XML中的IncudedLayout
的根视图,我设法使其工作.
It seems that you can't actually assign an OnClick handler to an <include>
tag directly. I managed to get it to work by adding another variable to IncludedLayout
s data binding class, and then assigning the OnClickListener to IncudedLayout
s root view in XML.
更改后,我的文件如下:
After the changes, my files looked like this:
class MyViewModel() {
val onClickFunction: OnClickListener = object:OnClickListener{
override fun onClick(v: View?) {
//Do something
}
}
}
包括布局
<layout>
<data>
<variable name="viewModel" type="full.package.name.MyViewModel"/>
</data>
<LinearLayout>
<include
layout="@layout/included_layout"
app:onClick="@{viewModel.onClickListener}"
app:customAttribute="@{`someText`}/>
</LinearLayout>
</layout>
包含的布局
<layout>
<data>
<variable name="customAttribute" type="String"/>
<variable name="onClick" type="android.view.View.OnClickListener"/>
</data>
<TextView
layout="@layout/included_layout"
android:text="@{customAttribute}"
android:onClick="@{onClick}"/>
</layout>
这篇关于数据绑定和包含的布局:找不到onClick的setter属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!