ViewGroupBindingAdapter.java revision 716ba89e7f459f49ea85070d4710c1d79d715298
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.animation.LayoutTransition; 19import android.annotation.TargetApi; 20import android.databinding.BindingAdapter; 21import android.databinding.BindingMethod; 22import android.databinding.BindingMethods; 23import android.os.Build; 24import android.view.View; 25import android.view.ViewGroup; 26import android.view.ViewGroup.OnHierarchyChangeListener; 27import android.view.animation.Animation; 28import android.view.animation.Animation.AnimationListener; 29 30@BindingMethods({ 31 @BindingMethod(type = android.view.ViewGroup.class, attribute = "android:alwaysDrawnWithCache", method = "setAlwaysDrawnWithCacheEnabled"), 32 @BindingMethod(type = android.view.ViewGroup.class, attribute = "android:animationCache", method = "setAnimationCacheEnabled"), 33 @BindingMethod(type = android.view.ViewGroup.class, attribute = "android:splitMotionEvents", method = "setMotionEventSplittingEnabled"), 34}) 35public class ViewGroupBindingAdapter { 36 37 @TargetApi(Build.VERSION_CODES.HONEYCOMB) 38 @BindingAdapter({"android:animateLayoutChanges"}) 39 public static void setAnimateLayoutChanges(ViewGroup view, boolean animate) { 40 if (animate) { 41 view.setLayoutTransition(new LayoutTransition()); 42 } else { 43 view.setLayoutTransition(null); 44 } 45 } 46 47 @BindingAdapter("android:onChildViewAdded") 48 public static void setListener(ViewGroup view, OnChildViewAdded listener) { 49 setListener(view, listener, null); 50 } 51 52 @BindingAdapter("android:onChildViewRemoved") 53 public static void setListener(ViewGroup view, OnChildViewRemoved listener) { 54 setListener(view, null, listener); 55 } 56 57 @BindingAdapter({"android:onChildViewAdded", "android:onChildViewRemoved"}) 58 public static void setListener(ViewGroup view, final OnChildViewAdded added, 59 final OnChildViewRemoved removed) { 60 if (added == null && removed == null) { 61 view.setOnHierarchyChangeListener(null); 62 } else { 63 view.setOnHierarchyChangeListener(new OnHierarchyChangeListener() { 64 @Override 65 public void onChildViewAdded(View parent, View child) { 66 if (added != null) { 67 added.onChildViewAdded(parent, child); 68 } 69 } 70 71 @Override 72 public void onChildViewRemoved(View parent, View child) { 73 if (removed != null) { 74 removed.onChildViewRemoved(parent, child); 75 } 76 } 77 }); 78 } 79 } 80 81 @BindingAdapter({"android:onAnimationStart", "android:onAnimationEnd", 82 "android:onAnimationRepeat"}) 83 public static void setListener(ViewGroup view, final OnAnimationStart start, 84 final OnAnimationEnd end, final OnAnimationRepeat repeat) { 85 if (start == null && end == null && repeat == null) { 86 view.setLayoutAnimationListener(null); 87 } else { 88 view.setLayoutAnimationListener(new AnimationListener() { 89 @Override 90 public void onAnimationStart(Animation animation) { 91 if (start != null) { 92 start.onAnimationStart(animation); 93 } 94 } 95 96 @Override 97 public void onAnimationEnd(Animation animation) { 98 if (end != null) { 99 end.onAnimationEnd(animation); 100 } 101 } 102 103 @Override 104 public void onAnimationRepeat(Animation animation) { 105 if (repeat != null) { 106 repeat.onAnimationRepeat(animation); 107 } 108 } 109 }); 110 } 111 } 112 113 @BindingAdapter({"android:onAnimationStart", "android:onAnimationEnd"}) 114 public static void setListener(ViewGroup view, final OnAnimationStart start, 115 final OnAnimationEnd end) { 116 setListener(view, start, end, null); 117 } 118 119 @BindingAdapter({"android:onAnimationEnd", "android:onAnimationRepeat"}) 120 public static void setListener(ViewGroup view, final OnAnimationEnd end, 121 final OnAnimationRepeat repeat) { 122 setListener(view, null, end, repeat); 123 } 124 125 @BindingAdapter({"android:onAnimationStart", "android:onAnimationRepeat"}) 126 public static void setListener(ViewGroup view, final OnAnimationStart start, 127 final OnAnimationRepeat repeat) { 128 setListener(view, start, null, repeat); 129 } 130 131 @BindingAdapter("android:onAnimationStart") 132 public static void setListener(ViewGroup view, final OnAnimationStart start) { 133 setListener(view, start, null, null); 134 } 135 136 @BindingAdapter("android:onAnimationEnd") 137 public static void setListener(ViewGroup view, final OnAnimationEnd end) { 138 setListener(view, null, end, null); 139 } 140 141 @BindingAdapter("android:onAnimationRepeat") 142 public static void setListener(ViewGroup view, final OnAnimationRepeat repeat) { 143 setListener(view, null, null, repeat); 144 } 145 146 public interface OnChildViewAdded { 147 void onChildViewAdded(View parent, View child); 148 } 149 150 public interface OnChildViewRemoved { 151 void onChildViewRemoved(View parent, View child); 152 } 153 154 public interface OnAnimationStart { 155 void onAnimationStart(Animation animation); 156 } 157 158 public interface OnAnimationEnd { 159 void onAnimationEnd(Animation animation); 160 } 161 162 public interface OnAnimationRepeat { 163 void onAnimationRepeat(Animation animation); 164 } 165} 166