1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package android.databinding;
17 
18 /**
19  * A listener implemented by all two-way bindings to be notified when a triggering change happens.
20  * For example, when there is a two-way binding for android:text, an implementation of
21  * <code>InverseBindingListener</code> will be generated in the layout's binding class.
22  * <pre>
23  * private static class InverseListenerTextView implements InverseBindingListener {
24  *     &commat;Override
25  *     public void onChange() {
26  *         mObj.setTextValue(mTextView.getText());
27  *     }
28  * }
29  * </pre>
30  * <p>
31  * A {@link BindingAdapter} should be used to assign the event listener.
32  * For example, <code>android:onTextChanged</code> will need to trigger the event listener
33  * for the <code>android:text</code> attribute.
34  * <pre>
35  * &commat;InverseBindingAdapter(attribute = "android:text", event = "android:textAttrChanged")
36  * public static void captureTextValue(TextView view, ObservableField&lt;CharSequence> value) {
37  *     CharSequence newValue = view.getText();
38  *     CharSequence oldValue = value.get();
39  *     if (oldValue == null) {
40  *         value.set(newValue);
41  *     } else if (!contentEquals(newValue, oldValue)) {
42  *         value.set(newValue);
43  *     }
44  * }
45  * &commat;BindingAdapter(value = {"android:beforeTextChanged", "android:onTextChanged",
46  *                          "android:afterTextChanged", "android:textAttrChanged"},
47  *                          requireAll = false)
48  * public static void setTextWatcher(TextView view, final BeforeTextChanged before,
49  *                                   final OnTextChanged on, final AfterTextChanged after,
50  *                                   final InverseBindingListener textAttrChanged) {
51  *     TextWatcher newValue = new TextWatcher() {
52  *         ...
53  *         &commat;Override
54  *         public void onTextChanged(CharSequence s, int start, int before, int count) {
55  *             if (on != null) {
56  *                 on.onTextChanged(s, start, before, count);
57  *             }
58  *             if (textAttrChanged != null) {
59  *                 textAttrChanged.onChange();
60  *             }
61  *         }
62  *     }
63  *     TextWatcher oldValue = ListenerUtil.trackListener(view, newValue, R.id.textWatcher);
64  *     if (oldValue != null) {
65  *         view.removeTextChangedListener(oldValue);
66  *     }
67  *     view.addTextChangedListener(newValue);
68  * }
69  * </pre>
70  */
71 public interface InverseBindingListener {
72     /**
73      * Notifies the data binding system that the attribute value has changed.
74      */
onChange()75     void onChange();
76 }
77