blob: f501e64292e2d9d839420f28481237e7fa261953 [file] [log] [blame]
Michael Kolbfe251992010-07-08 15:41:55 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
Michael Kolb27b08392010-10-04 12:49:23 -07004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
6 * the License at
Michael Kolbfe251992010-07-08 15:41:55 -07007 *
Michael Kolb27b08392010-10-04 12:49:23 -07008 * http://www.apache.org/licenses/LICENSE-2.0
Michael Kolbfe251992010-07-08 15:41:55 -07009 *
10 * Unless required by applicable law or agreed to in writing, software
Michael Kolb27b08392010-10-04 12:49:23 -070011 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
14 * the License.
Michael Kolbfe251992010-07-08 15:41:55 -070015 */
16
17package com.android.browser;
18
Michael Kolb27b08392010-10-04 12:49:23 -070019import android.animation.Animator;
Chet Haaseb3a00ab2010-10-14 07:01:46 -070020import android.animation.AnimatorListenerAdapter;
Michael Kolb27b08392010-10-04 12:49:23 -070021import android.animation.ObjectAnimator;
Michael Kolbfe251992010-07-08 15:41:55 -070022import android.content.Context;
Michael Kolbc3503762010-10-03 14:45:11 -070023import android.graphics.Canvas;
24import android.graphics.drawable.Drawable;
Michael Kolbfe251992010-07-08 15:41:55 -070025import android.util.AttributeSet;
26import android.view.View;
Michael Kolb27b08392010-10-04 12:49:23 -070027import android.view.animation.AccelerateInterpolator;
Michael Kolbfe251992010-07-08 15:41:55 -070028import android.widget.HorizontalScrollView;
29import android.widget.LinearLayout;
30
31/**
32 * custom view for displaying tabs in the tabbed title bar
33 */
34public class TabScrollView extends HorizontalScrollView {
35
Michael Kolb8233fac2010-10-26 16:08:53 -070036 private Context mContext;
Michael Kolbfe251992010-07-08 15:41:55 -070037 private LinearLayout mContentView;
Michael Kolbfe251992010-07-08 15:41:55 -070038 private int mSelected;
Michael Kolbc3503762010-10-03 14:45:11 -070039 private Drawable mArrowLeft;
40 private Drawable mArrowRight;
Michael Kolb27b08392010-10-04 12:49:23 -070041 private int mAnimationDuration;
Michael Kolb2b5a13a2010-12-09 14:13:42 -080042 private int mTabOverlap;
Michael Kolbfe251992010-07-08 15:41:55 -070043
44 /**
45 * @param context
46 * @param attrs
47 * @param defStyle
48 */
49 public TabScrollView(Context context, AttributeSet attrs, int defStyle) {
50 super(context, attrs, defStyle);
51 init(context);
52 }
53
54 /**
55 * @param context
56 * @param attrs
57 */
58 public TabScrollView(Context context, AttributeSet attrs) {
59 super(context, attrs);
60 init(context);
61 }
62
63 /**
64 * @param context
65 */
66 public TabScrollView(Context context) {
67 super(context);
68 init(context);
69 }
70
71 private void init(Context ctx) {
Michael Kolb8233fac2010-10-26 16:08:53 -070072 mContext = ctx;
Michael Kolb27b08392010-10-04 12:49:23 -070073 mAnimationDuration = ctx.getResources().getInteger(
74 R.integer.tab_animation_duration);
Michael Kolb2b5a13a2010-12-09 14:13:42 -080075 mTabOverlap = (int) ctx.getResources().getDimension(R.dimen.tab_overlap);
Michael Kolbfe251992010-07-08 15:41:55 -070076 setHorizontalScrollBarEnabled(false);
Michael Kolb2b5a13a2010-12-09 14:13:42 -080077 setOverScrollMode(OVER_SCROLL_NEVER);
78 mContentView = new TabLayout(mContext);
Michael Kolbfe251992010-07-08 15:41:55 -070079 mContentView.setOrientation(LinearLayout.HORIZONTAL);
Michael Kolb27b08392010-10-04 12:49:23 -070080 mContentView.setLayoutParams(
81 new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT));
Michael Kolbfe251992010-07-08 15:41:55 -070082 addView(mContentView);
83 mSelected = -1;
Michael Kolbc3503762010-10-03 14:45:11 -070084 mArrowLeft = ctx.getResources().getDrawable(R.drawable.ic_arrow_left);
85 mArrowRight = ctx.getResources().getDrawable(R.drawable.ic_arrow_right);
Michael Kolb27b08392010-10-04 12:49:23 -070086 // prevent ProGuard from removing the property methods
87 setScroll(getScroll());
Michael Kolbfe251992010-07-08 15:41:55 -070088 }
89
90 @Override
91 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
92 super.onLayout(changed, left, top, right, bottom);
93 ensureChildVisible(getSelectedTab());
94 }
95
96 void setSelectedTab(int position) {
97 View v = getSelectedTab();
98 if (v != null) {
Michael Kolbb7b115e2010-09-25 16:59:37 -070099 v.setActivated(false);
Michael Kolbfe251992010-07-08 15:41:55 -0700100 }
101 mSelected = position;
102 v = getSelectedTab();
103 if (v != null) {
Michael Kolbb7b115e2010-09-25 16:59:37 -0700104 v.setActivated(true);
Michael Kolbfe251992010-07-08 15:41:55 -0700105 }
106 requestLayout();
107 }
108
Michael Kolbebba8b42010-09-30 12:57:59 -0700109 int getChildIndex(View v) {
110 return mContentView.indexOfChild(v);
111 }
112
Michael Kolbfe251992010-07-08 15:41:55 -0700113 View getSelectedTab() {
114 if ((mSelected >= 0) && (mSelected < mContentView.getChildCount())) {
115 return mContentView.getChildAt(mSelected);
116 } else {
117 return null;
118 }
119 }
120
121 void clearTabs() {
122 mContentView.removeAllViews();
123 }
124
125 void addTab(View tab) {
126 mContentView.addView(tab);
Michael Kolbb7b115e2010-09-25 16:59:37 -0700127 tab.setActivated(false);
Michael Kolbfe251992010-07-08 15:41:55 -0700128 }
129
Michael Kolbfe251992010-07-08 15:41:55 -0700130 void removeTab(View tab) {
Michael Kolbbdc2a242010-09-07 13:48:07 -0700131 int ix = mContentView.indexOfChild(tab);
132 if (ix == mSelected) {
133 mSelected = -1;
134 } else if (ix < mSelected) {
135 mSelected--;
136 }
John Reck599e9102011-01-06 20:37:31 -0800137 mContentView.removeView(tab);
Michael Kolbfe251992010-07-08 15:41:55 -0700138 }
139
Michael Kolb27b08392010-10-04 12:49:23 -0700140 private void ensureChildVisible(View child) {
Michael Kolbfe251992010-07-08 15:41:55 -0700141 if (child != null) {
142 int childl = child.getLeft();
143 int childr = childl + child.getWidth();
144 int viewl = getScrollX();
145 int viewr = viewl + getWidth();
146 if (childl < viewl) {
147 // need scrolling to left
Michael Kolb27b08392010-10-04 12:49:23 -0700148 animateScroll(childl);
Michael Kolbfe251992010-07-08 15:41:55 -0700149 } else if (childr > viewr) {
150 // need scrolling to right
Michael Kolb27b08392010-10-04 12:49:23 -0700151 animateScroll(childr - viewr + viewl);
Michael Kolbfe251992010-07-08 15:41:55 -0700152 }
153 }
154 }
155
Michael Kolbc3503762010-10-03 14:45:11 -0700156 @Override
157 protected void dispatchDraw(Canvas canvas) {
158 super.dispatchDraw(canvas);
159 int l = getScrollX();
160 int r = l + getWidth();
161 int dis = 8;
162 if (l > 0) {
163 int aw = mArrowLeft.getIntrinsicWidth();
164 mArrowLeft.setBounds(l + dis, 0, l + dis + aw, getHeight());
165 mArrowLeft.draw(canvas);
166 }
167 if (r < mContentView.getWidth()) {
168 int aw = mArrowRight.getIntrinsicWidth();
169 mArrowRight.setBounds(r - dis - aw, 0, r - dis, getHeight());
170 mArrowRight.draw(canvas);
171 }
172 }
173
John Reck599e9102011-01-06 20:37:31 -0800174// TODO: These animations are broken and don't work correctly, removing for now
175// as animateOut is actually causing issues
176// private void animateIn(View tab) {
177// ObjectAnimator animator = ObjectAnimator.ofInt(tab, "TranslationX", 500, 0);
178// animator.setDuration(mAnimationDuration);
179// animator.start();
180// }
181//
182// private void animateOut(final View tab) {
183// ObjectAnimator animator = ObjectAnimator.ofInt(
184// tab, "TranslationX", 0, getScrollX() - tab.getRight());
185// animator.setDuration(mAnimationDuration);
186// animator.addListener(new AnimatorListenerAdapter() {
187// @Override
188// public void onAnimationEnd(Animator animation) {
189// mContentView.removeView(tab);
190// }
191// });
192// animator.setInterpolator(new AccelerateInterpolator());
193// animator.start();
194// }
Michael Kolb27b08392010-10-04 12:49:23 -0700195
196 private void animateScroll(int newscroll) {
Chet Haaseb3a00ab2010-10-14 07:01:46 -0700197 ObjectAnimator animator = ObjectAnimator.ofInt(this, "scroll", getScrollX(), newscroll);
198 animator.setDuration(mAnimationDuration);
Michael Kolb27b08392010-10-04 12:49:23 -0700199 animator.start();
200 }
201
202 /**
203 * required for animation
204 */
205 public void setScroll(int newscroll) {
206 scrollTo(newscroll, getScrollY());
207 }
208
209 /**
210 * required for animation
211 */
212 public int getScroll() {
213 return getScrollX();
214 }
215
Michael Kolb2b5a13a2010-12-09 14:13:42 -0800216 class TabLayout extends LinearLayout {
217
218 public TabLayout(Context context) {
219 super(context);
220 setChildrenDrawingOrderEnabled(true);
221 }
222
223 @Override
224 protected void onMeasure(int hspec, int vspec) {
225 super.onMeasure(hspec, vspec);
226 int w = getMeasuredWidth();
227 w -= Math.max(0, mContentView.getChildCount() - 1) * mTabOverlap;
228 setMeasuredDimension(w, getMeasuredHeight());
229 }
230
231 @Override
232 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
233 super.onLayout(changed, left, top, right, bottom);
234 if (getChildCount() > 1) {
235 int nextLeft = getChildAt(0).getRight() - mTabOverlap;
236 for (int i = 1; i < getChildCount(); i++) {
237 View tab = getChildAt(i);
238 int w = tab.getRight() - tab.getLeft();
239 tab.layout(nextLeft, tab.getTop(), nextLeft + w, tab.getBottom());
240 nextLeft += w - mTabOverlap;
241 }
242 }
243 }
244
245 @Override
246 protected int getChildDrawingOrder(int count, int i) {
247 int next = -1;
248 if ((i == (count - 1)) && (mSelected >= 0)) {
249 next = mSelected;
250 } else {
251 next = count - i - 1;
252 if (next <= mSelected) {
253 next--;
254 }
255 }
256 return next;
257 }
258
259 }
260
Michael Kolbfe251992010-07-08 15:41:55 -0700261}