NumberPickerBindingAdapter.java revision 3b920788e90bb0abe615a5d5c899915f0014444b
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.adapters;
17
18import android.databinding.BindingAdapter;
19import android.databinding.BindingMethod;
20import android.databinding.BindingMethods;
21import android.databinding.InverseBindingListener;
22import android.databinding.InverseBindingMethod;
23import android.databinding.InverseBindingMethods;
24import android.widget.CompoundButton;
25import android.widget.CompoundButton.OnCheckedChangeListener;
26import android.widget.NumberPicker;
27import android.widget.NumberPicker.OnValueChangeListener;
28
29@BindingMethods({
30        @BindingMethod(type = NumberPicker.class, attribute = "android:format", method = "setFormatter"),
31        @BindingMethod(type = NumberPicker.class, attribute = "android:onScrollStateChange", method = "setOnScrollListener"),
32})
33@InverseBindingMethods({
34        @InverseBindingMethod(type = NumberPicker.class, attribute = "android:value"),
35})
36public class NumberPickerBindingAdapter {
37
38    @BindingAdapter("android:value")
39    public static void setValue(NumberPicker view, int value) {
40        if (view.getValue() != value) {
41            view.setValue(value);
42        }
43    }
44
45    @BindingAdapter(value = {"android:onValueChange", "android:valueAttrChanged"},
46            requireAll = false)
47    public static void setListeners(NumberPicker view, final OnValueChangeListener listener,
48            final InverseBindingListener attrChange) {
49        if (attrChange == null) {
50            view.setOnValueChangedListener(listener);
51        } else {
52            view.setOnValueChangedListener(new OnValueChangeListener() {
53                @Override
54                public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
55                    if (listener != null) {
56                        listener.onValueChange(picker, oldVal, newVal);
57                    }
58                    attrChange.onChange();
59                }
60            });
61        }
62    }
63}
64