MultiArgTestAdapter.java revision e9b33bac04bb1ce1444d7f1744fcec1ecd3a57da
1/* 2 * Copyright (C) 2015 The Android Open Source Project 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * http://www.apache.org/licenses/LICENSE-2.0 7 * Unless required by applicable law or agreed to in writing, software 8 * distributed under the License is distributed on an "AS IS" BASIS, 9 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 * See the License for the specific language governing permissions and 11 * limitations under the License. 12 */ 13 14package android.databinding.testapp.adapter; 15 16 17import android.databinding.BaseObservable; 18import android.databinding.Bindable; 19import android.databinding.BindingAdapter; 20import android.view.View; 21import android.databinding.testapp.BR; 22import android.widget.TextView; 23 24import org.apache.commons.lang3.StringUtils; 25 26public class MultiArgTestAdapter { 27 28 public static String join(BaseMultiBindingClass... classes) { 29 StringBuilder sb = new StringBuilder(); 30 for(BaseMultiBindingClass instance : classes) { 31 sb.append(instance == null ? "??" : instance.getValue()); 32 } 33 return sb.toString(); 34 } 35 36 public static String join(String... strings) { 37 StringBuilder sb = new StringBuilder(); 38 for(String str : strings) { 39 sb.append(str == null ? "??" : str); 40 } 41 return sb.toString(); 42 43 } 44 45 @BindingAdapter(attributes={"android:class1", "android:class2"}) 46 public static void setBoth(TextView view, MultiBindingClass1 class1, 47 MultiBindingClass2 class2) { 48 view.setText(join(class1, class2)); 49 } 50 51 @BindingAdapter(attributes={"android:class1str", "android:class2str"}) 52 public static void setBoth(TextView view, String str1, 53 String str2) { 54 view.setText(join(str1, str2)); 55 } 56 57 @BindingAdapter(attributes={"android:class1"}) 58 public static void setClass1(TextView view, MultiBindingClass1 class1) { 59 view.setText(class1.getValue()); 60 } 61 62 @BindingAdapter(attributes={"android:classStr"}) 63 public static void setClassStr(TextView view, String str) { 64 view.setText(str); 65 } 66 67 @BindingAdapter(attributes={"android:class2"}) 68 public static void setClass2(TextView view, MultiBindingClass2 class2) { 69 view.setText(class2.getValue()); 70 } 71 72 public static class MultiBindingClass1 extends BaseMultiBindingClass { 73 74 } 75 76 public static class MultiBindingClass2 extends BaseMultiBindingClass { 77 78 } 79 80 public static class BaseMultiBindingClass extends BaseObservable { 81 View mSetOn; 82 @Bindable 83 String mValue; 84 public View getSetOn() { 85 return mSetOn; 86 } 87 88 public String getValue() { 89 return mValue; 90 } 91 92 public void setValue(String value, boolean notify) { 93 mValue = value; 94 if (notify) { 95 notifyPropertyChanged(BR.value); 96 } 97 } 98 99 public void clear() { 100 mSetOn = null; 101 } 102 } 103} 104