Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to place android BindingAdapter method?

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?

like image 961
chaser Avatar asked Nov 28 '16 13:11

chaser


People also ask

How do I use BindingAdapter on Android?

To create a custom binding adapter, you need to create an extension function of the view that will use the adapter. Then, you add the @BindingAdapter annotation. You have to indicate the name of the view attribute that will execute this adapter as a parameter in the annotation.

Which is the correct way to reference bound data in the XML layout?

Layout Binding expressions Expressions in the XML layout files are assigned to a value of the attribute properties using the “ @{} " syntax. We just need to use the basic syntax @{} in an assignment expression.

How do you use binding adapters?

Binding adapters are responsible for making the appropriate framework calls to set values. One example is setting a property value like calling the setText() method. Another example is setting an event listener like calling the setOnClickListener() method.


2 Answers

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.

like image 198
chaser Avatar answered Oct 08 '22 08:10

chaser


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);     } } 
like image 29
Manza Avatar answered Oct 08 '22 09:10

Manza