Where to place android BindingAdapter method?

后端 未结 2 1189
攒了一身酷
攒了一身酷 2021-02-04 23:59

This has to be the most basic of question but after a full day of reading tutorials and the documentation here I can\'t seem to understand where to put these methods. None of th

相关标签:
2条回答
  • 2021-02-05 00:46

    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.

    0 讨论(0)
  • 2021-02-05 00:58

    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);
        }
    }
    
    0 讨论(0)
提交回复
热议问题