Most visited

Recently visited

InverseBindingListener

public interface InverseBindingListener

android.databinding.InverseBindingListener


由所有双向绑定实现的监听器在发生触发更改时得到通知。 例如,当对android:text有双向绑定时,将在布局的绑定类中生成一个InverseBindingListener的实现。

 private static class InverseListenerTextView implements InverseBindingListener {
     @Override
     public void onChange() {
         mObj.setTextValue(mTextView.getText());
     }
 }
 

应使用BindingAdapter分配事件侦听器。 例如, android:onTextChanged将需要触发android:text属性的事件侦听器。

 @InverseBindingAdapter(attribute = "android:text", event = "android:textAttrChanged")
 public static void captureTextValue(TextView view, ObservableField<CharSequence> value) {
     CharSequence newValue = view.getText();
     CharSequence oldValue = value.get();
     if (oldValue == null) {
         value.set(newValue);
     } else if (!contentEquals(newValue, oldValue)) {
         value.set(newValue);
     }
 }
 @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);
 }
 

Summary

Public methods

abstract void onChange()

通知数据绑定系统该属性值已更改。

Public methods

onChange

void onChange ()

通知数据绑定系统该属性值已更改。

Hooray!