13561e3e665698843b1c664385a842e779198960bGeorge Mount/*
23561e3e665698843b1c664385a842e779198960bGeorge Mount * Copyright (C) 2015 The Android Open Source Project
33561e3e665698843b1c664385a842e779198960bGeorge Mount *
43561e3e665698843b1c664385a842e779198960bGeorge Mount * Licensed under the Apache License, Version 2.0 (the "License");
53561e3e665698843b1c664385a842e779198960bGeorge Mount * you may not use this file except in compliance with the License.
63561e3e665698843b1c664385a842e779198960bGeorge Mount * You may obtain a copy of the License at
73561e3e665698843b1c664385a842e779198960bGeorge Mount *
83561e3e665698843b1c664385a842e779198960bGeorge Mount *      http://www.apache.org/licenses/LICENSE-2.0
93561e3e665698843b1c664385a842e779198960bGeorge Mount *
103561e3e665698843b1c664385a842e779198960bGeorge Mount * Unless required by applicable law or agreed to in writing, software
113561e3e665698843b1c664385a842e779198960bGeorge Mount * distributed under the License is distributed on an "AS IS" BASIS,
123561e3e665698843b1c664385a842e779198960bGeorge Mount * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
133561e3e665698843b1c664385a842e779198960bGeorge Mount * See the License for the specific language governing permissions and
143561e3e665698843b1c664385a842e779198960bGeorge Mount * limitations under the License.
153561e3e665698843b1c664385a842e779198960bGeorge Mount */
16fead9ca09b117136b35bc5bf137340a754f9edddGeorge Mountpackage android.databinding.adapters;
173561e3e665698843b1c664385a842e779198960bGeorge Mount
183561e3e665698843b1c664385a842e779198960bGeorge Mountimport android.animation.LayoutTransition;
193561e3e665698843b1c664385a842e779198960bGeorge Mountimport android.annotation.TargetApi;
20fead9ca09b117136b35bc5bf137340a754f9edddGeorge Mountimport android.databinding.BindingAdapter;
21fead9ca09b117136b35bc5bf137340a754f9edddGeorge Mountimport android.databinding.BindingMethod;
22fead9ca09b117136b35bc5bf137340a754f9edddGeorge Mountimport android.databinding.BindingMethods;
233561e3e665698843b1c664385a842e779198960bGeorge Mountimport android.os.Build;
24716ba89e7f459f49ea85070d4710c1d79d715298George Mountimport android.view.View;
253561e3e665698843b1c664385a842e779198960bGeorge Mountimport android.view.ViewGroup;
26716ba89e7f459f49ea85070d4710c1d79d715298George Mountimport android.view.ViewGroup.OnHierarchyChangeListener;
27716ba89e7f459f49ea85070d4710c1d79d715298George Mountimport android.view.animation.Animation;
28716ba89e7f459f49ea85070d4710c1d79d715298George Mountimport android.view.animation.Animation.AnimationListener;
293561e3e665698843b1c664385a842e779198960bGeorge Mount
303561e3e665698843b1c664385a842e779198960bGeorge Mount@BindingMethods({
31c619d8f69127c1200103d8119101c5f0675661d0George Mount        @BindingMethod(type = android.view.ViewGroup.class, attribute = "android:alwaysDrawnWithCache", method = "setAlwaysDrawnWithCacheEnabled"),
32c619d8f69127c1200103d8119101c5f0675661d0George Mount        @BindingMethod(type = android.view.ViewGroup.class, attribute = "android:animationCache", method = "setAnimationCacheEnabled"),
33c619d8f69127c1200103d8119101c5f0675661d0George Mount        @BindingMethod(type = android.view.ViewGroup.class, attribute = "android:splitMotionEvents", method = "setMotionEventSplittingEnabled"),
343561e3e665698843b1c664385a842e779198960bGeorge Mount})
353561e3e665698843b1c664385a842e779198960bGeorge Mountpublic class ViewGroupBindingAdapter {
363561e3e665698843b1c664385a842e779198960bGeorge Mount
373561e3e665698843b1c664385a842e779198960bGeorge Mount    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
3810960eb5f73fd587c2f8d18cfc61873c04017512George Mount    @BindingAdapter({"android:animateLayoutChanges"})
393561e3e665698843b1c664385a842e779198960bGeorge Mount    public static void setAnimateLayoutChanges(ViewGroup view, boolean animate) {
403561e3e665698843b1c664385a842e779198960bGeorge Mount        if (animate) {
413561e3e665698843b1c664385a842e779198960bGeorge Mount            view.setLayoutTransition(new LayoutTransition());
423561e3e665698843b1c664385a842e779198960bGeorge Mount        } else {
433561e3e665698843b1c664385a842e779198960bGeorge Mount            view.setLayoutTransition(null);
443561e3e665698843b1c664385a842e779198960bGeorge Mount        }
453561e3e665698843b1c664385a842e779198960bGeorge Mount    }
46716ba89e7f459f49ea85070d4710c1d79d715298George Mount
4796b22e7bbbf942aea1079dc8e8d0c4657663e5a7George Mount    @BindingAdapter(value = {"android:onChildViewAdded", "android:onChildViewRemoved"},
4896b22e7bbbf942aea1079dc8e8d0c4657663e5a7George Mount            requireAll = false)
49716ba89e7f459f49ea85070d4710c1d79d715298George Mount    public static void setListener(ViewGroup view, final OnChildViewAdded added,
50716ba89e7f459f49ea85070d4710c1d79d715298George Mount            final OnChildViewRemoved removed) {
51716ba89e7f459f49ea85070d4710c1d79d715298George Mount        if (added == null && removed == null) {
52716ba89e7f459f49ea85070d4710c1d79d715298George Mount            view.setOnHierarchyChangeListener(null);
53716ba89e7f459f49ea85070d4710c1d79d715298George Mount        } else {
54716ba89e7f459f49ea85070d4710c1d79d715298George Mount            view.setOnHierarchyChangeListener(new OnHierarchyChangeListener() {
55716ba89e7f459f49ea85070d4710c1d79d715298George Mount                @Override
56716ba89e7f459f49ea85070d4710c1d79d715298George Mount                public void onChildViewAdded(View parent, View child) {
57716ba89e7f459f49ea85070d4710c1d79d715298George Mount                    if (added != null) {
58716ba89e7f459f49ea85070d4710c1d79d715298George Mount                        added.onChildViewAdded(parent, child);
59716ba89e7f459f49ea85070d4710c1d79d715298George Mount                    }
60716ba89e7f459f49ea85070d4710c1d79d715298George Mount                }
61716ba89e7f459f49ea85070d4710c1d79d715298George Mount
62716ba89e7f459f49ea85070d4710c1d79d715298George Mount                @Override
63716ba89e7f459f49ea85070d4710c1d79d715298George Mount                public void onChildViewRemoved(View parent, View child) {
64716ba89e7f459f49ea85070d4710c1d79d715298George Mount                    if (removed != null) {
65716ba89e7f459f49ea85070d4710c1d79d715298George Mount                        removed.onChildViewRemoved(parent, child);
66716ba89e7f459f49ea85070d4710c1d79d715298George Mount                    }
67716ba89e7f459f49ea85070d4710c1d79d715298George Mount                }
68716ba89e7f459f49ea85070d4710c1d79d715298George Mount            });
69716ba89e7f459f49ea85070d4710c1d79d715298George Mount        }
70716ba89e7f459f49ea85070d4710c1d79d715298George Mount    }
71716ba89e7f459f49ea85070d4710c1d79d715298George Mount
7296b22e7bbbf942aea1079dc8e8d0c4657663e5a7George Mount    @BindingAdapter(value = {"android:onAnimationStart", "android:onAnimationEnd",
7396b22e7bbbf942aea1079dc8e8d0c4657663e5a7George Mount            "android:onAnimationRepeat"}, requireAll = false)
74716ba89e7f459f49ea85070d4710c1d79d715298George Mount    public static void setListener(ViewGroup view, final OnAnimationStart start,
75716ba89e7f459f49ea85070d4710c1d79d715298George Mount            final OnAnimationEnd end, final OnAnimationRepeat repeat) {
76716ba89e7f459f49ea85070d4710c1d79d715298George Mount        if (start == null && end == null && repeat == null) {
77716ba89e7f459f49ea85070d4710c1d79d715298George Mount            view.setLayoutAnimationListener(null);
78716ba89e7f459f49ea85070d4710c1d79d715298George Mount        } else {
79716ba89e7f459f49ea85070d4710c1d79d715298George Mount            view.setLayoutAnimationListener(new AnimationListener() {
80716ba89e7f459f49ea85070d4710c1d79d715298George Mount                @Override
81716ba89e7f459f49ea85070d4710c1d79d715298George Mount                public void onAnimationStart(Animation animation) {
82716ba89e7f459f49ea85070d4710c1d79d715298George Mount                    if (start != null) {
83716ba89e7f459f49ea85070d4710c1d79d715298George Mount                        start.onAnimationStart(animation);
84716ba89e7f459f49ea85070d4710c1d79d715298George Mount                    }
85716ba89e7f459f49ea85070d4710c1d79d715298George Mount                }
86716ba89e7f459f49ea85070d4710c1d79d715298George Mount
87716ba89e7f459f49ea85070d4710c1d79d715298George Mount                @Override
88716ba89e7f459f49ea85070d4710c1d79d715298George Mount                public void onAnimationEnd(Animation animation) {
89716ba89e7f459f49ea85070d4710c1d79d715298George Mount                    if (end != null) {
90716ba89e7f459f49ea85070d4710c1d79d715298George Mount                        end.onAnimationEnd(animation);
91716ba89e7f459f49ea85070d4710c1d79d715298George Mount                    }
92716ba89e7f459f49ea85070d4710c1d79d715298George Mount                }
93716ba89e7f459f49ea85070d4710c1d79d715298George Mount
94716ba89e7f459f49ea85070d4710c1d79d715298George Mount                @Override
95716ba89e7f459f49ea85070d4710c1d79d715298George Mount                public void onAnimationRepeat(Animation animation) {
96716ba89e7f459f49ea85070d4710c1d79d715298George Mount                    if (repeat != null) {
97716ba89e7f459f49ea85070d4710c1d79d715298George Mount                        repeat.onAnimationRepeat(animation);
98716ba89e7f459f49ea85070d4710c1d79d715298George Mount                    }
99716ba89e7f459f49ea85070d4710c1d79d715298George Mount                }
100716ba89e7f459f49ea85070d4710c1d79d715298George Mount            });
101716ba89e7f459f49ea85070d4710c1d79d715298George Mount        }
102716ba89e7f459f49ea85070d4710c1d79d715298George Mount    }
103716ba89e7f459f49ea85070d4710c1d79d715298George Mount
104716ba89e7f459f49ea85070d4710c1d79d715298George Mount    public interface OnChildViewAdded {
105716ba89e7f459f49ea85070d4710c1d79d715298George Mount        void onChildViewAdded(View parent, View child);
106716ba89e7f459f49ea85070d4710c1d79d715298George Mount    }
107716ba89e7f459f49ea85070d4710c1d79d715298George Mount
108716ba89e7f459f49ea85070d4710c1d79d715298George Mount    public interface OnChildViewRemoved {
109716ba89e7f459f49ea85070d4710c1d79d715298George Mount        void onChildViewRemoved(View parent, View child);
110716ba89e7f459f49ea85070d4710c1d79d715298George Mount    }
111716ba89e7f459f49ea85070d4710c1d79d715298George Mount
112716ba89e7f459f49ea85070d4710c1d79d715298George Mount    public interface OnAnimationStart {
113716ba89e7f459f49ea85070d4710c1d79d715298George Mount        void onAnimationStart(Animation animation);
114716ba89e7f459f49ea85070d4710c1d79d715298George Mount    }
115716ba89e7f459f49ea85070d4710c1d79d715298George Mount
116716ba89e7f459f49ea85070d4710c1d79d715298George Mount    public interface OnAnimationEnd {
117716ba89e7f459f49ea85070d4710c1d79d715298George Mount        void onAnimationEnd(Animation animation);
118716ba89e7f459f49ea85070d4710c1d79d715298George Mount    }
119716ba89e7f459f49ea85070d4710c1d79d715298George Mount
120716ba89e7f459f49ea85070d4710c1d79d715298George Mount    public interface OnAnimationRepeat {
121716ba89e7f459f49ea85070d4710c1d79d715298George Mount        void onAnimationRepeat(Animation animation);
122716ba89e7f459f49ea85070d4710c1d79d715298George Mount    }
1233561e3e665698843b1c664385a842e779198960bGeorge Mount}
124