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.RatingBar; 23import android.widget.RatingBar.OnRatingBarChangeListener; 24 25@InverseBindingMethods({ 26 @InverseBindingMethod(type = RatingBar.class, attribute = "android:rating"), 27}) 28public class RatingBarBindingAdapter { 29 @BindingAdapter("android:rating") 30 public static void setRating(RatingBar view, float rating) { 31 if (view.getRating() != rating) { 32 view.setRating(rating); 33 } 34 } 35 36 @BindingAdapter(value = {"android:onRatingChanged", "android:ratingAttrChanged"}, 37 requireAll = false) 38 public static void setListeners(RatingBar view, final OnRatingBarChangeListener listener, 39 final InverseBindingListener ratingChange) { 40 if (ratingChange == null) { 41 view.setOnRatingBarChangeListener(listener); 42 } else { 43 view.setOnRatingBarChangeListener(new OnRatingBarChangeListener() { 44 @Override 45 public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) { 46 if (listener != null) { 47 listener.onRatingChanged(ratingBar, rating, fromUser); 48 } 49 ratingChange.onChange(); 50 } 51 }); 52 } 53 } 54} 55