不同控件对不同数据类型的databinding,持续更新

TextView:

​text: String:​

<TextView
...
android:text="@{item.name}" />

​text: Int(kotlin)->String​

android:text="@{String.valueOf(item.id)}"
android:text="@{item.id.toString()}"

​textColor: Int​

android:textColor="@{itemModel.setColor(context)}"
fun setColor(context: Context): Int? {
val colorId = when (state) {
MainConstants.TASK_STATE_ALARM -> {
R.color.word_theme_red
}
MainConstants.TASK_STATE_PROCEED -> {
R.color.word_theme_blue
}
MainConstants.TASK_STATE_FINISH -> {
R.color.word_theme_green
}
else -> null
}
return colorId?.let { ContextCompat.getColor(context, it) }
}

ImageView:

​src: Drawable​

android:src="@{itemModel.setHeadPhoto(context)}"
fun setHeadPhoto(context: Context): Drawable? {
return HeadPhoto.getListHeadPhoto(prisonerSex, state)?.let {
ContextCompat.getDrawable(context,it)//重点
}
}

​app:imageUrl​​​​Image(这里必需自己写BindingAdapter)​

app:imageUrl="@{item.images}"
//Databinding加载图片
@BindingAdapter("imageUrl")
@JvmStatic
fun loadImage(view: ImageView?, url: String?) {
if (!TextUtils.isEmpty(url)) {
Glide.with(view?.context!!).load(url).dontAnimate().placeholder(R.mipmap.settings_person).into(view)
}
}