Most visited

Recently visited

InverseBindingAdapter

public abstract @interface InverseBindingAdapter
implements Annotation

android.databinding.InverseBindingAdapter


InverseBindingAdapter与用于在设置从视图收集的值时检索视图值的方法相关联。 这类似于BindingAdapter s:

 @InverseBindingAdapter(attribute = "android:text", event = "android:textAttrChanged")
 public static String captureTextValue(TextView view, CharSequence originalValue) {
     CharSequence newValue = view.getText();
     CharSequence oldValue = value.get();
     if (oldValue == null) {
         value.set(newValue);
     } else if (!contentEquals(newValue, oldValue)) {
         value.set(newValue);
     }
 }
 

事件的默认值是带有“AttrChanged”后缀的属性名称。 在上面的例子中,即使没有提供,默认值也会是android:textAttrChanged

事件属性用于通知数据绑定系统该值已更改。 开发人员通常会创建一个BindingAdapter来分配事件。 例如:

 @BindingAdapter(value = {"android:beforeTextChanged", "android:onTextChanged",
                          "android:afterTextChanged", "android:textAttrChanged"},
                          requireAll = false)
 public static void setTextWatcher(TextView view, final BeforeTextChanged before,
                                   final OnTextChanged on, final AfterTextChanged after,
                                   final InverseBindingListener textAttrChanged) {
     TextWatcher newValue = new TextWatcher() {
         ...
         @Override
         public void onTextChanged(CharSequence s, int start, int before, int count) {
             if (on != null) {
                 on.onTextChanged(s, start, before, count);
             }
             if (textAttrChanged != null) {
                 textAttrChanged.onChange();
             }
         }
     }
     TextWatcher oldValue = ListenerUtil.trackListener(view, newValue, R.id.textWatcher);
     if (oldValue != null) {
         view.removeTextChangedListener(oldValue);
     }
     view.addTextChangedListener(newValue);
 }
 

BindingAdapter一样,InverseBindingAdapter方法也可能需要 DataBindingComponent作为第一个参数,并且可能是一个实例方法,其实例从 DataBindingComponent检索。

也可以看看:

Summary

Inherited methods

From interface java.lang.annotation.Annotation

Hooray!