xml中的代码
<ImageView
android:id="@+id/image_iv"
android:layout_width="80dp"
android:layout_height="80dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:srcResource="@{viewmodel.count.get()}"
/>
bindingAdapter中的代码,这些都已经准备就绪就是编译不过
@BindingAdapter("app:srcResource")
@JvmStatic fun srcResource(view : ImageView,count: Int){
val color = if(count > 10 ) Color.RED else Color.BLUE
ImageViewCompat.setImageTintList(view, ColorStateList.valueOf(color))
view.setImageResource(R.drawable.ic_person_black_96dp)
}
编译的时候报了以下错误:
* What went wrong:
Execution failed for task ':app:compileDebugJavaWithJavac'.
> android.databinding.tool.util.LoggedErrorException: Found data binding errors.
****/ data binding error ****msg:Cannot find the setter for attribute 'app:srcResourcet' with parameter type int on android.widget.ImageView.
****\ data binding error ****
这是因为写好的注解代码没有编译的原因。需要在app gradle中的添加一行代码:
apply plugin: 'kotlin-kapt'