1612997fe2e41366573855f56898b27d4c8787244George Mount/*
2612997fe2e41366573855f56898b27d4c8787244George Mount * Copyright (C) 2015 The Android Open Source Project
3612997fe2e41366573855f56898b27d4c8787244George Mount *
4612997fe2e41366573855f56898b27d4c8787244George Mount * Licensed under the Apache License, Version 2.0 (the "License");
5612997fe2e41366573855f56898b27d4c8787244George Mount * you may not use this file except in compliance with the License.
6612997fe2e41366573855f56898b27d4c8787244George Mount * You may obtain a copy of the License at
7612997fe2e41366573855f56898b27d4c8787244George Mount *
8612997fe2e41366573855f56898b27d4c8787244George Mount *      http://www.apache.org/licenses/LICENSE-2.0
9612997fe2e41366573855f56898b27d4c8787244George Mount *
10612997fe2e41366573855f56898b27d4c8787244George Mount * Unless required by applicable law or agreed to in writing, software
11612997fe2e41366573855f56898b27d4c8787244George Mount * distributed under the License is distributed on an "AS IS" BASIS,
12612997fe2e41366573855f56898b27d4c8787244George Mount * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13612997fe2e41366573855f56898b27d4c8787244George Mount * See the License for the specific language governing permissions and
14612997fe2e41366573855f56898b27d4c8787244George Mount * limitations under the License.
15612997fe2e41366573855f56898b27d4c8787244George Mount */
16fead9ca09b117136b35bc5bf137340a754f9edddGeorge Mountpackage android.databinding;
17612997fe2e41366573855f56898b27d4c8787244George Mount
18612997fe2e41366573855f56898b27d4c8787244George Mountimport java.lang.annotation.ElementType;
19612997fe2e41366573855f56898b27d4c8787244George Mountimport java.lang.annotation.Target;
20612997fe2e41366573855f56898b27d4c8787244George Mount
21c9a5d6f140f732ca0ff279a4b1ee315072e1c422George Mount/**
22c9a5d6f140f732ca0ff279a4b1ee315072e1c422George Mount * BindingAdapter is applied to methods that are used to manipulate how values with expressions
23c9a5d6f140f732ca0ff279a4b1ee315072e1c422George Mount * are set to views. The simplest example is to have a public static method that takes the view
24c9a5d6f140f732ca0ff279a4b1ee315072e1c422George Mount * and the value to set:
25c9a5d6f140f732ca0ff279a4b1ee315072e1c422George Mount * <p><pre>
26c9a5d6f140f732ca0ff279a4b1ee315072e1c422George Mount *<code>@BindingAdapter("android:bufferType")
27c9a5d6f140f732ca0ff279a4b1ee315072e1c422George Mount * public static void setBufferType(TextView view, TextView.BufferType bufferType) {
28c9a5d6f140f732ca0ff279a4b1ee315072e1c422George Mount *     view.setText(view.getText(), bufferType);
29c9a5d6f140f732ca0ff279a4b1ee315072e1c422George Mount * }</code></pre>
30c9a5d6f140f732ca0ff279a4b1ee315072e1c422George Mount * In the above example, when android:bufferType is used on a TextView, the method
31c9a5d6f140f732ca0ff279a4b1ee315072e1c422George Mount * setBufferType is called.
32c9a5d6f140f732ca0ff279a4b1ee315072e1c422George Mount * <p>
33c9a5d6f140f732ca0ff279a4b1ee315072e1c422George Mount * It is also possible to take previously set values, if the old values are listed first:
34c9a5d6f140f732ca0ff279a4b1ee315072e1c422George Mount * <p><pre>
35c9a5d6f140f732ca0ff279a4b1ee315072e1c422George Mount *<code>@BindingAdapter("android:onLayoutChange")
36c9a5d6f140f732ca0ff279a4b1ee315072e1c422George Mount * public static void setOnLayoutChangeListener(View view, View.OnLayoutChangeListener oldValue,
37c9a5d6f140f732ca0ff279a4b1ee315072e1c422George Mount *                                              View.OnLayoutChangeListener newValue) {
38c9a5d6f140f732ca0ff279a4b1ee315072e1c422George Mount *     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
39c9a5d6f140f732ca0ff279a4b1ee315072e1c422George Mount *         if (oldValue != null) {
40c9a5d6f140f732ca0ff279a4b1ee315072e1c422George Mount *             view.removeOnLayoutChangeListener(oldValue);
41c9a5d6f140f732ca0ff279a4b1ee315072e1c422George Mount *         }
42c9a5d6f140f732ca0ff279a4b1ee315072e1c422George Mount *         if (newValue != null) {
43c9a5d6f140f732ca0ff279a4b1ee315072e1c422George Mount *             view.addOnLayoutChangeListener(newValue);
44c9a5d6f140f732ca0ff279a4b1ee315072e1c422George Mount *         }
45c9a5d6f140f732ca0ff279a4b1ee315072e1c422George Mount *     }
46c9a5d6f140f732ca0ff279a4b1ee315072e1c422George Mount * }</code></pre>
47c9a5d6f140f732ca0ff279a4b1ee315072e1c422George Mount * When a binding adapter may also take multiple attributes, it will only be called when all
48c9a5d6f140f732ca0ff279a4b1ee315072e1c422George Mount * attributes associated with the binding adapter have binding expressions associated with them.
49c9a5d6f140f732ca0ff279a4b1ee315072e1c422George Mount * This is useful when there are unusual interactions between attributes. For example:
50c9a5d6f140f732ca0ff279a4b1ee315072e1c422George Mount * <p><pre>
51c9a5d6f140f732ca0ff279a4b1ee315072e1c422George Mount *<code>@BindingAdapter({"android:onClick", "android:clickable"})
52c9a5d6f140f732ca0ff279a4b1ee315072e1c422George Mount * public static void setOnClick(View view, View.OnClickListener clickListener,
53c9a5d6f140f732ca0ff279a4b1ee315072e1c422George Mount *                               boolean clickable) {
54c9a5d6f140f732ca0ff279a4b1ee315072e1c422George Mount *     view.setOnClickListener(clickListener);
55c9a5d6f140f732ca0ff279a4b1ee315072e1c422George Mount *     view.setClickable(clickable);
56c9a5d6f140f732ca0ff279a4b1ee315072e1c422George Mount * }</code></pre>
57c9a5d6f140f732ca0ff279a4b1ee315072e1c422George Mount * The order of the parameters must match the order of the attributes in values in the
58c9a5d6f140f732ca0ff279a4b1ee315072e1c422George Mount * BindingAdapter.
59c9a5d6f140f732ca0ff279a4b1ee315072e1c422George Mount * <p>
60c9a5d6f140f732ca0ff279a4b1ee315072e1c422George Mount * A binding adapter may optionally take a class extending DataBindingComponent as the first
61c9a5d6f140f732ca0ff279a4b1ee315072e1c422George Mount * parameter as well. If it does, it will be passed the value passed in during binding, either
62c9a5d6f140f732ca0ff279a4b1ee315072e1c422George Mount * directly in the inflate method or indirectly, using the value from
63c9a5d6f140f732ca0ff279a4b1ee315072e1c422George Mount * {@link DataBindingUtil#getDefaultComponent()}.
64c9a5d6f140f732ca0ff279a4b1ee315072e1c422George Mount * <p>
65c9a5d6f140f732ca0ff279a4b1ee315072e1c422George Mount * If a binding adapter is an instance method, the generated DataBindingComponent will have
66c9a5d6f140f732ca0ff279a4b1ee315072e1c422George Mount * a getter to retrieve an instance of the BindingAdapter's class to use to call the method.
67c9a5d6f140f732ca0ff279a4b1ee315072e1c422George Mount */
68612997fe2e41366573855f56898b27d4c8787244George Mount@Target(ElementType.METHOD)
69612997fe2e41366573855f56898b27d4c8787244George Mountpublic @interface BindingAdapter {
70c9a5d6f140f732ca0ff279a4b1ee315072e1c422George Mount
71c9a5d6f140f732ca0ff279a4b1ee315072e1c422George Mount    /**
72c9a5d6f140f732ca0ff279a4b1ee315072e1c422George Mount     * @return The attributes associated with this binding adapter.
73c9a5d6f140f732ca0ff279a4b1ee315072e1c422George Mount     */
7410960eb5f73fd587c2f8d18cfc61873c04017512George Mount    String[] value();
7596b22e7bbbf942aea1079dc8e8d0c4657663e5a7George Mount
7696b22e7bbbf942aea1079dc8e8d0c4657663e5a7George Mount    /**
7796b22e7bbbf942aea1079dc8e8d0c4657663e5a7George Mount     * Whether every attribute must be assigned a binding expression or if some
7896b22e7bbbf942aea1079dc8e8d0c4657663e5a7George Mount     * can be absent. When this is false, the BindingAdapter will be called
7996b22e7bbbf942aea1079dc8e8d0c4657663e5a7George Mount     * when at least one associated attribute has a binding expression. The attributes
8096b22e7bbbf942aea1079dc8e8d0c4657663e5a7George Mount     * for which there was no binding expression (even a normal XML value) will
8196b22e7bbbf942aea1079dc8e8d0c4657663e5a7George Mount     * cause the associated parameter receive the Java default value. Care must be
8296b22e7bbbf942aea1079dc8e8d0c4657663e5a7George Mount     * taken to ensure that a default value is not confused with a valid XML value.
8396b22e7bbbf942aea1079dc8e8d0c4657663e5a7George Mount     *
8496b22e7bbbf942aea1079dc8e8d0c4657663e5a7George Mount     * @return whether or not every attribute must be assigned a binding expression. The default
8596b22e7bbbf942aea1079dc8e8d0c4657663e5a7George Mount     *         value is true.
8696b22e7bbbf942aea1079dc8e8d0c4657663e5a7George Mount     */
8796b22e7bbbf942aea1079dc8e8d0c4657663e5a7George Mount    boolean requireAll() default true;
88612997fe2e41366573855f56898b27d4c8787244George Mount}
89