InverseBindingMethod.java revision d9ecf9ada33d18a9b78ac81ffb4a4326d660c099
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 */
16package android.databinding;
17
18import java.lang.annotation.ElementType;
19import java.lang.annotation.Target;
20
21/**
22 * InverseBindingMethod is used to identify how to listen for changes to a View property and which
23 * getter method to call. InverseBindingMethod should be associated with any class as part of
24 * {@link InverseBindingMethods}.
25 * <p>
26 * <pre>
27 * &commat;InverseBindingMethods({&commat;InverseBindingMethod(
28 *     type = android.widget.TextView.class,
29 *     attribute = "android:text",
30 *     event = "android:textAttrChanged",
31 *     method = "getText")})
32 * public class MyTextViewBindingAdapters { ... }
33 * </pre>
34 * <p>
35 * <code>method</code> is optional. If it isn't provided, the attribute name is used to
36 * find the method name, either prefixing with "is" or "get". For the attribute
37 * <code>android:text</code>, data binding will search for a
38 * <code>public CharSequence getText()</code> method on {@link android.widget.TextView}.
39 * <p>
40 * <code>event</code> is optional. If it isn't provided, the event name is assigned the
41 * attribute name suffixed with <code>AttrChanged</code>. For the <code>android:text</code>
42 * attribute, the default event name would be <code>android:textAttrChanged</code>. The event
43 * should be set using a {@link BindingAdapter}. For example:
44 * <pre>
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 * @see InverseBindingAdapter
72 * @see InverseBindingListener
73 */
74@Target(ElementType.ANNOTATION_TYPE)
75public @interface InverseBindingMethod {
76
77    /**
78     * The View type that is associated with the attribute.
79     */
80    Class type();
81
82    /**
83     * The attribute that supports two-way binding.
84     */
85    String attribute();
86
87    /**
88     * The event used to notify the data binding system that the attribute value has changed.
89     * Defaults to attribute() + "AttrChanged"
90     */
91    String event() default "";
92
93    /**
94     * The getter method to retrieve the attribute value from the View. The default is
95     * the bean method name based on the attribute name.
96     */
97    String method() default "";
98}
99