The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
| 17 | package android.widget; |
| 18 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 19 | import android.content.Context; |
| 20 | import android.util.AttributeSet; |
| 21 | import android.view.View; |
| 22 | import 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 | */ |
| 30 | public 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 | /** |
| 70 | * Returns the next view to be displayed. |
| 71 | * |
| 72 | * @return the view that will be displayed after the next views flip. |
| 73 | */ |
| 74 | public View getNextView() { |
| 75 | int which = mWhichChild == 0 ? 1 : 0; |
| 76 | return getChildAt(which); |
| 77 | } |
| 78 | |
| 79 | private View obtainView() { |
| 80 | View child = mFactory.makeView(); |
| 81 | LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
| 82 | if (lp == null) { |
Romain Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 83 | lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 84 | } |
| 85 | addView(child, lp); |
| 86 | return child; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Sets the factory used to create the two views between which the |
| 91 | * ViewSwitcher will flip. Instead of using a factory, you can call |
| 92 | * {@link #addView(android.view.View, int, android.view.ViewGroup.LayoutParams)} |
| 93 | * twice. |
| 94 | * |
| 95 | * @param factory the view factory used to generate the switcher's content |
| 96 | */ |
| 97 | public void setFactory(ViewFactory factory) { |
| 98 | mFactory = factory; |
| 99 | obtainView(); |
| 100 | obtainView(); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Reset the ViewSwitcher to hide all of the existing views and to make it |
| 105 | * think that the first time animation has not yet played. |
| 106 | */ |
| 107 | public void reset() { |
| 108 | mFirstTime = true; |
| 109 | View v; |
| 110 | v = getChildAt(0); |
| 111 | if (v != null) { |
| 112 | v.setVisibility(View.GONE); |
| 113 | } |
| 114 | v = getChildAt(1); |
| 115 | if (v != null) { |
| 116 | v.setVisibility(View.GONE); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Creates views in a ViewSwitcher. |
| 122 | */ |
| 123 | public interface ViewFactory { |
| 124 | /** |
| 125 | * Creates a new {@link android.view.View} to be added in a |
| 126 | * {@link android.widget.ViewSwitcher}. |
| 127 | * |
| 128 | * @return a {@link android.view.View} |
| 129 | */ |
| 130 | View makeView(); |
| 131 | } |
| 132 | } |
| 133 | |