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.adapters; 17 18 import android.databinding.BindingAdapter; 19 import android.databinding.BindingMethod; 20 import android.databinding.BindingMethods; 21 import android.databinding.InverseBindingListener; 22 import android.databinding.InverseBindingMethod; 23 import android.databinding.InverseBindingMethods; 24 import android.widget.CompoundButton; 25 import android.widget.CompoundButton.OnCheckedChangeListener; 26 import android.widget.NumberPicker; 27 import 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 }) 36 public 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