public abstract @interface BindingAdapter
implements Annotation
android.databinding.BindingAdapter |
BindingAdapter适用于用于处理将表达式的值如何设置为视图的方法。 最简单的例子是使用一个公共静态方法来设置视图和值:
@BindingAdapter("android:bufferType")
public static void setBufferType(TextView view, TextView.BufferType bufferType) {
view.setText(view.getText(), bufferType);
}
In the above example, when android:bufferType is used on a TextView, the method setBufferType is called.
如果旧的值先列出,也可以取先前设定的值:
@BindingAdapter("android:onLayoutChange")
public static void setOnLayoutChangeListener(View view, View.OnLayoutChangeListener oldValue,
View.OnLayoutChangeListener newValue) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
if (oldValue != null) {
view.removeOnLayoutChangeListener(oldValue);
}
if (newValue != null) {
view.addOnLayoutChangeListener(newValue);
}
}
}
When a binding adapter may also take multiple attributes, it will only be called when all attributes associated with the binding adapter have binding expressions associated with them. This is useful when there are unusual interactions between attributes. For example:
@BindingAdapter({"android:onClick", "android:clickable"})
public static void setOnClick(View view, View.OnClickListener clickListener,
boolean clickable) {
view.setOnClickListener(clickListener);
view.setClickable(clickable);
}
The order of the parameters must match the order of the attributes in values in the BindingAdapter.
绑定适配器也可以选择扩展DataBindingComponent作为第一个参数。 如果是这样,它将通过绑定期间传入的值,直接在膨胀方法中或间接使用getDefaultComponent()
的值。
如果一个绑定适配器是一个实例方法,那么生成的DataBindingComponent将有一个getter来检索BindingAdapter类的一个实例,以用来调用该方法。
Inherited methods |
|
---|---|
From interface java.lang.annotation.Annotation
|