ViewGroupBindingAdapter.java revision 96b22e7bbbf942aea1079dc8e8d0c4657663e5a7
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(value = {"android:onChildViewAdded", "android:onChildViewRemoved"},
48            requireAll = false)
49    public static void setListener(ViewGroup view, final OnChildViewAdded added,
50            final OnChildViewRemoved removed) {
51        if (added == null && removed == null) {
52            view.setOnHierarchyChangeListener(null);
53        } else {
54            view.setOnHierarchyChangeListener(new OnHierarchyChangeListener() {
55                @Override
56                public void onChildViewAdded(View parent, View child) {
57                    if (added != null) {
58                        added.onChildViewAdded(parent, child);
59                    }
60                }
61
62                @Override
63                public void onChildViewRemoved(View parent, View child) {
64                    if (removed != null) {
65                        removed.onChildViewRemoved(parent, child);
66                    }
67                }
68            });
69        }
70    }
71
72    @BindingAdapter(value = {"android:onAnimationStart", "android:onAnimationEnd",
73            "android:onAnimationRepeat"}, requireAll = false)
74    public static void setListener(ViewGroup view, final OnAnimationStart start,
75            final OnAnimationEnd end, final OnAnimationRepeat repeat) {
76        if (start == null && end == null && repeat == null) {
77            view.setLayoutAnimationListener(null);
78        } else {
79            view.setLayoutAnimationListener(new AnimationListener() {
80                @Override
81                public void onAnimationStart(Animation animation) {
82                    if (start != null) {
83                        start.onAnimationStart(animation);
84                    }
85                }
86
87                @Override
88                public void onAnimationEnd(Animation animation) {
89                    if (end != null) {
90                        end.onAnimationEnd(animation);
91                    }
92                }
93
94                @Override
95                public void onAnimationRepeat(Animation animation) {
96                    if (repeat != null) {
97                        repeat.onAnimationRepeat(animation);
98                    }
99                }
100            });
101        }
102    }
103
104    public interface OnChildViewAdded {
105        void onChildViewAdded(View parent, View child);
106    }
107
108    public interface OnChildViewRemoved {
109        void onChildViewRemoved(View parent, View child);
110    }
111
112    public interface OnAnimationStart {
113        void onAnimationStart(Animation animation);
114    }
115
116    public interface OnAnimationEnd {
117        void onAnimationEnd(Animation animation);
118    }
119
120    public interface OnAnimationRepeat {
121        void onAnimationRepeat(Animation animation);
122    }
123}
124