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.InverseBindingListener; 20import android.databinding.InverseBindingMethod; 21import android.databinding.InverseBindingMethods; 22import android.widget.SeekBar; 23import android.widget.SeekBar.OnSeekBarChangeListener; 24 25@InverseBindingMethods({ 26 @InverseBindingMethod(type = SeekBar.class, attribute = "android:progress"), 27}) 28public class SeekBarBindingAdapter { 29 30 @BindingAdapter("android:progress") 31 public static void setProgress(SeekBar view, int progress) { 32 if (progress != view.getProgress()) { 33 view.setProgress(progress); 34 } 35 } 36 37 @BindingAdapter(value = {"android:onStartTrackingTouch", "android:onStopTrackingTouch", 38 "android:onProgressChanged", "android:progressAttrChanged"}, requireAll = false) 39 public static void setOnSeekBarChangeListener(SeekBar view, final OnStartTrackingTouch start, 40 final OnStopTrackingTouch stop, final OnProgressChanged progressChanged, 41 final InverseBindingListener attrChanged) { 42 if (start == null && stop == null && progressChanged == null && attrChanged == null) { 43 view.setOnSeekBarChangeListener(null); 44 } else { 45 view.setOnSeekBarChangeListener(new OnSeekBarChangeListener() { 46 @Override 47 public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { 48 if (progressChanged != null) { 49 progressChanged.onProgressChanged(seekBar, progress, fromUser); 50 } 51 if (attrChanged != null) { 52 attrChanged.onChange(); 53 } 54 } 55 56 @Override 57 public void onStartTrackingTouch(SeekBar seekBar) { 58 if (start != null) { 59 start.onStartTrackingTouch(seekBar); 60 } 61 } 62 63 @Override 64 public void onStopTrackingTouch(SeekBar seekBar) { 65 if (stop != null) { 66 stop.onStopTrackingTouch(seekBar); 67 } 68 } 69 }); 70 } 71 } 72 73 public interface OnStartTrackingTouch { 74 void onStartTrackingTouch(SeekBar seekBar); 75 } 76 77 public interface OnStopTrackingTouch { 78 void onStopTrackingTouch(SeekBar seekBar); 79 } 80 81 public interface OnProgressChanged { 82 void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser); 83 } 84} 85