在java中使用dataBinding展示图片很简单,只需要配置一个静态的BindingAdapter就可以了。
如图:
在kotlin中使用:
首先:kotlin中没有static关键字,但是提供了companion object{}代码块和使用object关键字
object关键字声明一种特殊的类,这个类只有一个实例,因此看起来整个类就好像是一个对象一样,这里把类声明时的class关键字改成了object,这个类里面的成员默认都是static的.
@JvmStatic注解:与伴生对象搭配使用,将变量和函数声明为真正的JVM静态成员。
接下来上代码:
注意:一定要加上@JvmStatic,否则会报下面的错
java.lang.IllegalStateException: Required DataBindingComponent is null in class FragmentPersonalCenterBinding. A BindingAdapter in classcool_enterprise.zhixing.com.classcool_enterprise.utils.DataBindingUtils.Companion is not static and requires an object to use, retrieved from the DataBindingComponent. If you don't use an inflation method taking a DataBindingComponent, use DataBindingUtil.setDefaultComponent or make all BindingAdapter methods static.