Where to place android BindingAdapter method?
Asked Answered
E

2

44

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 the guides mention where this thing is placed it just mentions to use the annotation on a static method. What static method? Any static method at all regardless of class? What is a good practice? do I create a CustomBinding class to host all these static methods?

So long as I have the method has a BindingAdapter annotation and the first parameter is a View, it will work?

I assume if the first parameter is of type View I can place the binding attribute on any type of views and it will trigger the method? So if I have specific view say EditText, does this mean the method is only called when the attribute is found in an EditText view in the layout file?

Ellis answered 28/11, 2016 at 13:43 Comment(1)
see how it is done with already existing adapters, like TextViewBindingAdapter and parent folderHeaviness
E
32

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.

Ellis answered 29/11, 2016 at 12:4 Comment(2)
Thanks a lot! You saved hours of my timeOrthoepy
or you can go right here about defining the recommended ways (if you using kotlin) to optimize @BindingAdapter methodMorass
S
5

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);
    }
}
Stairs answered 28/11, 2016 at 17:42 Comment(7)
Please explain whether this is the only way to do it or is this your way? There are some BindingAdapters that are grouped in a class, eg android.googlesource.com/platform/frameworks/data-binding/+/…Ellis
I don't think model class is not a good place to put this BindingAdapter because it is not meant to couple with the model. For example, I can't do two @BindingAdapter ("android:src") - one to scale the image to 200x200 and another to scale image to 400x400. I think it might be better to put them in a class specifically for binding (eg. BindingAdapterUtils).Failing
@Failing do you have any idea while using Binding Adapter without custom views? I tried to bring to other file but don't know how to put it in tagDefence
@TruongHieu, you do not need a custom view. You can use it for anything. Just create a new file (eg. BindingUtils) and place a static method there. Remember to declare @BindingAdapter (eg. @BindingAdapter ("android:src")).Failing
Sure I tried it with custom attribute successfully. But why I didnt see inspection of that custom attribute? @FailingDefence
Hi Truong, I don't quite understand your question. Perhaps open a new question thread with more detail explanation :)Failing
another example: github.com/google/iosched/blob/…Phase

© 2022 - 2024 — McMap. All rights reserved.