Where to place android BindingAdapter method?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-03 10:28:43

After navigating through the internet I've finally found some info from one of the developers themselves. I wish they would have been more clear on the basics in the documentation.

Quote:

Binding adapters are annotated methods in any class that are used to do just this. Typically, you’d organize your adapters into [-a] classes based on the target View type.

This obviously means that at compile time all methods in any class with the annotation BindingAdapter will generate the BindingAdapter.

You place it in your model class.

Example:

XML:

    <data>

    <variable
        name="item"
        type="com.yourpackage.Model"/>
      </data>
         ......

           <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@{item.resId}"/>

Model:

public class Model {

@DrawableRes
private final int resId;

public Model(int resId) {
    this.resId = resId;
}

public int getResId() {
    return resId;
}

@BindingAdapter ("android:src")
public static void setImageResource(ImageView imageView, int resource){
    imageView.setImageResource(resource);
}

}

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!