1/*
2 * Copyright (C) 2006 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 */
16
17package android.widget;
18
19import android.content.Context;
20import android.util.AttributeSet;
21import android.view.View;
22import android.view.ViewGroup;
23
24/**
25 * {@link ViewAnimator} that switches between two views, and has a factory
26 * from which these views are created.  You can either use the factory to
27 * create the views, or add them yourself.  A ViewSwitcher can only have two
28 * child views, of which only one is shown at a time.
29 */
30public class ViewSwitcher extends ViewAnimator {
31    /**
32     * The factory used to create the two children.
33     */
34    ViewFactory mFactory;
35
36    /**
37     * Creates a new empty ViewSwitcher.
38     *
39     * @param context the application's environment
40     */
41    public ViewSwitcher(Context context) {
42        super(context);
43    }
44
45    /**
46     * Creates a new empty ViewSwitcher for the given context and with the
47     * specified set attributes.
48     *
49     * @param context the application environment
50     * @param attrs a collection of attributes
51     */
52    public ViewSwitcher(Context context, AttributeSet attrs) {
53        super(context, attrs);
54    }
55
56    /**
57     * {@inheritDoc}
58     *
59     * @throws IllegalStateException if this switcher already contains two children
60     */
61    @Override
62    public void addView(View child, int index, ViewGroup.LayoutParams params) {
63        if (getChildCount() >= 2) {
64            throw new IllegalStateException("Can't add more than 2 views to a ViewSwitcher");
65        }
66        super.addView(child, index, params);
67    }
68
69    @Override
70    public CharSequence getAccessibilityClassName() {
71        return ViewSwitcher.class.getName();
72    }
73
74    /**
75     * Returns the next view to be displayed.
76     *
77     * @return the view that will be displayed after the next views flip.
78     */
79    public View getNextView() {
80        int which = mWhichChild == 0 ? 1 : 0;
81        return getChildAt(which);
82    }
83
84    private View obtainView() {
85        View child = mFactory.makeView();
86        LayoutParams lp = (LayoutParams) child.getLayoutParams();
87        if (lp == null) {
88            lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
89        }
90        addView(child, lp);
91        return child;
92    }
93
94    /**
95     * Sets the factory used to create the two views between which the
96     * ViewSwitcher will flip. Instead of using a factory, you can call
97     * {@link #addView(android.view.View, int, android.view.ViewGroup.LayoutParams)}
98     * twice.
99     *
100     * @param factory the view factory used to generate the switcher's content
101     */
102    public void setFactory(ViewFactory factory) {
103        mFactory = factory;
104        obtainView();
105        obtainView();
106    }
107
108    /**
109     * Reset the ViewSwitcher to hide all of the existing views and to make it
110     * think that the first time animation has not yet played.
111     */
112    public void reset() {
113        mFirstTime = true;
114        View v;
115        v = getChildAt(0);
116        if (v != null) {
117            v.setVisibility(View.GONE);
118        }
119        v = getChildAt(1);
120        if (v != null) {
121            v.setVisibility(View.GONE);
122        }
123    }
124
125    /**
126     * Creates views in a ViewSwitcher.
127     */
128    public interface ViewFactory {
129        /**
130         * Creates a new {@link android.view.View} to be added in a
131         * {@link android.widget.ViewSwitcher}.
132         *
133         * @return a {@link android.view.View}
134         */
135        View makeView();
136    }
137}
138
139