Andrews DataBinding (d) custom properties

Android DataBinding (a) Basic Usage  
Android DataBinding (two) event handler  
Android DataBinding (three) Observable 
Android DataBinding (d) custom properties (paper) 
Android DataBinding (five) Custom View's way binding  
Android DataBinding (six) EditText tie given TextChangedListener and FocusChangeListener

Foreword

DataBinding when the calls View, setXxx method. For example, the code below

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@{user.name}" />

Time bound, calls the setText (String text) method. 
Provided that the TextView have setText (String text) method, no setText method, or a method setText method signature is not the same, it is not enough. 
So, not all properties can be set by DataBinding.

When there is no method, there are two ways

1. If there is a similar method, but the method is not the same name or method signature, existing methods can be invoked by setting.

For example, android: onClick, View is not setOnClick method, but there are setOnClickListener method, and the method signature is the same. 
This time we can use BindingMethod to be a transit.

@BindingMethods({
        @BindingMethod(type = View.class, attribute = "android:onClick", method = "setOnClickListener")
})

@BindingMethods to define the class name on it. 
Of course, if you just want to rename setter, it can also be achieved by @BindingMethods.

For example, android: paddingLeft, View is not setPaddingLeft method, only setPadding method. 
This time you can set a custom method, and add @BindingAdapter in the above methods.

@BindingAdapter("android:paddingLeft")
public static void setPaddingLeft(View view, int padding) {
   view.setPadding(padding,
                   view.getPaddingTop(),
                   view.getPaddingRight(),
                   view.getPaddingBottom());
}

2. No similar manner, adding a corresponding method.  
For example, app: xxx attribute 
if the method signature is the app: xxx property worth in the set, then the method can be defined directly setXxx. 
If the method signature like the above setPaddingLeft as incoming themselves also need to View, then in addition to the definition of setXxx method, also you need to add @BindingAdapter ( "android: xxx")

DataBinding Library provides a number of custom attributes

Fortunately, some common attributes, such as the above-mentioned android: onClick and android: paddingLeft, Library have to help you set up, you do not need to be used directly in the settings. (Refer to specifically android.databinding.adapters.ViewBindingAdapter)

Multi-attribute binding

The same method, a plurality of attributes may be provided at the same time

@BindingAdapter({"bind:imageUrl", "bind:error"})
public static void loadImage(ImageView view, String url, Drawable error) {
   Picasso.with(view.getContext()).load(url).error(error).into(view);
}

Picasso also need to read the image imageUrl and error. At the same time when the definition was called indispensable.

The above example is set at the same time when it calls, if it is defined in which one when he calls, then just add a requireAll = false on OK.

@BindingAdapter(value = {"android:onViewDetachedFromWindow", "android:onViewAttachedToWindow"},
            requireAll = false)
    public static void setOnAttachStateChangeListener(View view,
            final OnViewDetachedFromWindow detach, final OnViewAttachedToWindow attach) {
}

setOnAttachStateChangeListener in android: onViewDetachedFromWindow or android: when one onViewAttachedToWindow which calls on the call.

发布了24 篇原创文章 · 获赞 5 · 访问量 2万+

Guess you like

Origin blog.csdn.net/qq_26923265/article/details/82745466