Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
Michael Kolb | 27b0839 | 2010-10-04 12:49:23 -0700 | [diff] [blame] | 4 | * 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 Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 7 | * |
Michael Kolb | 27b0839 | 2010-10-04 12:49:23 -0700 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
Michael Kolb | 27b0839 | 2010-10-04 12:49:23 -0700 | [diff] [blame] | 11 | * 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 Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | package com.android.browser; |
| 18 | |
Michael Kolb | 27b0839 | 2010-10-04 12:49:23 -0700 | [diff] [blame] | 19 | import android.animation.Animator; |
Chet Haase | b3a00ab | 2010-10-14 07:01:46 -0700 | [diff] [blame] | 20 | import android.animation.AnimatorListenerAdapter; |
Michael Kolb | 27b0839 | 2010-10-04 12:49:23 -0700 | [diff] [blame] | 21 | import android.animation.ObjectAnimator; |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 22 | import android.content.Context; |
Michael Kolb | c350376 | 2010-10-03 14:45:11 -0700 | [diff] [blame] | 23 | import android.graphics.Canvas; |
| 24 | import android.graphics.drawable.Drawable; |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 25 | import android.util.AttributeSet; |
| 26 | import android.view.View; |
Michael Kolb | 27b0839 | 2010-10-04 12:49:23 -0700 | [diff] [blame] | 27 | import android.view.animation.AccelerateInterpolator; |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 28 | import android.widget.HorizontalScrollView; |
| 29 | import android.widget.LinearLayout; |
| 30 | |
| 31 | /** |
| 32 | * custom view for displaying tabs in the tabbed title bar |
| 33 | */ |
| 34 | public class TabScrollView extends HorizontalScrollView { |
| 35 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 36 | private Context mContext; |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 37 | private LinearLayout mContentView; |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 38 | private int mSelected; |
Michael Kolb | c350376 | 2010-10-03 14:45:11 -0700 | [diff] [blame] | 39 | private Drawable mArrowLeft; |
| 40 | private Drawable mArrowRight; |
Michael Kolb | 27b0839 | 2010-10-04 12:49:23 -0700 | [diff] [blame] | 41 | private int mAnimationDuration; |
Michael Kolb | 2b5a13a | 2010-12-09 14:13:42 -0800 | [diff] [blame] | 42 | private int mTabOverlap; |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 43 | |
| 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 Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 72 | mContext = ctx; |
Michael Kolb | 27b0839 | 2010-10-04 12:49:23 -0700 | [diff] [blame] | 73 | mAnimationDuration = ctx.getResources().getInteger( |
| 74 | R.integer.tab_animation_duration); |
Michael Kolb | 2b5a13a | 2010-12-09 14:13:42 -0800 | [diff] [blame] | 75 | mTabOverlap = (int) ctx.getResources().getDimension(R.dimen.tab_overlap); |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 76 | setHorizontalScrollBarEnabled(false); |
Michael Kolb | 2b5a13a | 2010-12-09 14:13:42 -0800 | [diff] [blame] | 77 | setOverScrollMode(OVER_SCROLL_NEVER); |
| 78 | mContentView = new TabLayout(mContext); |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 79 | mContentView.setOrientation(LinearLayout.HORIZONTAL); |
Michael Kolb | 27b0839 | 2010-10-04 12:49:23 -0700 | [diff] [blame] | 80 | mContentView.setLayoutParams( |
| 81 | new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT)); |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 82 | addView(mContentView); |
| 83 | mSelected = -1; |
Michael Kolb | c350376 | 2010-10-03 14:45:11 -0700 | [diff] [blame] | 84 | mArrowLeft = ctx.getResources().getDrawable(R.drawable.ic_arrow_left); |
| 85 | mArrowRight = ctx.getResources().getDrawable(R.drawable.ic_arrow_right); |
Michael Kolb | 27b0839 | 2010-10-04 12:49:23 -0700 | [diff] [blame] | 86 | // prevent ProGuard from removing the property methods |
| 87 | setScroll(getScroll()); |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 88 | } |
| 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 Kolb | b7b115e | 2010-09-25 16:59:37 -0700 | [diff] [blame] | 99 | v.setActivated(false); |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 100 | } |
| 101 | mSelected = position; |
| 102 | v = getSelectedTab(); |
| 103 | if (v != null) { |
Michael Kolb | b7b115e | 2010-09-25 16:59:37 -0700 | [diff] [blame] | 104 | v.setActivated(true); |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 105 | } |
| 106 | requestLayout(); |
| 107 | } |
| 108 | |
Michael Kolb | ebba8b4 | 2010-09-30 12:57:59 -0700 | [diff] [blame] | 109 | int getChildIndex(View v) { |
| 110 | return mContentView.indexOfChild(v); |
| 111 | } |
| 112 | |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 113 | 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 Kolb | b7b115e | 2010-09-25 16:59:37 -0700 | [diff] [blame] | 127 | tab.setActivated(false); |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 130 | void removeTab(View tab) { |
Michael Kolb | bdc2a24 | 2010-09-07 13:48:07 -0700 | [diff] [blame] | 131 | int ix = mContentView.indexOfChild(tab); |
| 132 | if (ix == mSelected) { |
| 133 | mSelected = -1; |
| 134 | } else if (ix < mSelected) { |
| 135 | mSelected--; |
| 136 | } |
John Reck | 599e910 | 2011-01-06 20:37:31 -0800 | [diff] [blame] | 137 | mContentView.removeView(tab); |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Michael Kolb | 27b0839 | 2010-10-04 12:49:23 -0700 | [diff] [blame] | 140 | private void ensureChildVisible(View child) { |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 141 | 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 Kolb | 27b0839 | 2010-10-04 12:49:23 -0700 | [diff] [blame] | 148 | animateScroll(childl); |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 149 | } else if (childr > viewr) { |
| 150 | // need scrolling to right |
Michael Kolb | 27b0839 | 2010-10-04 12:49:23 -0700 | [diff] [blame] | 151 | animateScroll(childr - viewr + viewl); |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | } |
| 155 | |
Michael Kolb | c350376 | 2010-10-03 14:45:11 -0700 | [diff] [blame] | 156 | @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 Reck | 599e910 | 2011-01-06 20:37:31 -0800 | [diff] [blame] | 174 | // 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 Kolb | 27b0839 | 2010-10-04 12:49:23 -0700 | [diff] [blame] | 195 | |
| 196 | private void animateScroll(int newscroll) { |
Chet Haase | b3a00ab | 2010-10-14 07:01:46 -0700 | [diff] [blame] | 197 | ObjectAnimator animator = ObjectAnimator.ofInt(this, "scroll", getScrollX(), newscroll); |
| 198 | animator.setDuration(mAnimationDuration); |
Michael Kolb | 27b0839 | 2010-10-04 12:49:23 -0700 | [diff] [blame] | 199 | 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 Kolb | 2b5a13a | 2010-12-09 14:13:42 -0800 | [diff] [blame] | 216 | 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 Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 261 | } |