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.ObjectAnimator; |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 20 | import android.content.Context; |
| 21 | import android.util.AttributeSet; |
| 22 | import android.view.View; |
| 23 | import android.widget.HorizontalScrollView; |
| 24 | import android.widget.LinearLayout; |
| 25 | |
| 26 | /** |
| 27 | * custom view for displaying tabs in the tabbed title bar |
| 28 | */ |
| 29 | public class TabScrollView extends HorizontalScrollView { |
| 30 | |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 31 | private LinearLayout mContentView; |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 32 | private int mSelected; |
Michael Kolb | 27b0839 | 2010-10-04 12:49:23 -0700 | [diff] [blame] | 33 | private int mAnimationDuration; |
Michael Kolb | 2b5a13a | 2010-12-09 14:13:42 -0800 | [diff] [blame] | 34 | private int mTabOverlap; |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 35 | |
| 36 | /** |
| 37 | * @param context |
| 38 | * @param attrs |
| 39 | * @param defStyle |
| 40 | */ |
| 41 | public TabScrollView(Context context, AttributeSet attrs, int defStyle) { |
| 42 | super(context, attrs, defStyle); |
| 43 | init(context); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * @param context |
| 48 | * @param attrs |
| 49 | */ |
| 50 | public TabScrollView(Context context, AttributeSet attrs) { |
| 51 | super(context, attrs); |
| 52 | init(context); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * @param context |
| 57 | */ |
| 58 | public TabScrollView(Context context) { |
| 59 | super(context); |
| 60 | init(context); |
| 61 | } |
| 62 | |
| 63 | private void init(Context ctx) { |
Michael Kolb | 27b0839 | 2010-10-04 12:49:23 -0700 | [diff] [blame] | 64 | mAnimationDuration = ctx.getResources().getInteger( |
| 65 | R.integer.tab_animation_duration); |
Michael Kolb | 2b5a13a | 2010-12-09 14:13:42 -0800 | [diff] [blame] | 66 | mTabOverlap = (int) ctx.getResources().getDimension(R.dimen.tab_overlap); |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 67 | setHorizontalScrollBarEnabled(false); |
Michael Kolb | 2b5a13a | 2010-12-09 14:13:42 -0800 | [diff] [blame] | 68 | setOverScrollMode(OVER_SCROLL_NEVER); |
Romain Guy | e398956 | 2011-01-27 12:06:29 -0800 | [diff] [blame^] | 69 | mContentView = new TabLayout(ctx); |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 70 | mContentView.setOrientation(LinearLayout.HORIZONTAL); |
Michael Kolb | 27b0839 | 2010-10-04 12:49:23 -0700 | [diff] [blame] | 71 | mContentView.setLayoutParams( |
| 72 | new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT)); |
Michael Kolb | f558f0d | 2011-01-13 19:24:18 -0800 | [diff] [blame] | 73 | mContentView.setPadding( |
| 74 | (int) ctx.getResources().getDimension(R.dimen.tab_first_padding_left), |
| 75 | 0, 0, 0); |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 76 | addView(mContentView); |
| 77 | mSelected = -1; |
Michael Kolb | 27b0839 | 2010-10-04 12:49:23 -0700 | [diff] [blame] | 78 | // prevent ProGuard from removing the property methods |
| 79 | setScroll(getScroll()); |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | @Override |
| 83 | protected void onLayout(boolean changed, int left, int top, int right, int bottom) { |
| 84 | super.onLayout(changed, left, top, right, bottom); |
| 85 | ensureChildVisible(getSelectedTab()); |
| 86 | } |
| 87 | |
| 88 | void setSelectedTab(int position) { |
| 89 | View v = getSelectedTab(); |
| 90 | if (v != null) { |
Michael Kolb | b7b115e | 2010-09-25 16:59:37 -0700 | [diff] [blame] | 91 | v.setActivated(false); |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 92 | } |
| 93 | mSelected = position; |
| 94 | v = getSelectedTab(); |
| 95 | if (v != null) { |
Michael Kolb | b7b115e | 2010-09-25 16:59:37 -0700 | [diff] [blame] | 96 | v.setActivated(true); |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 97 | } |
| 98 | requestLayout(); |
| 99 | } |
| 100 | |
Michael Kolb | ebba8b4 | 2010-09-30 12:57:59 -0700 | [diff] [blame] | 101 | int getChildIndex(View v) { |
| 102 | return mContentView.indexOfChild(v); |
| 103 | } |
| 104 | |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 105 | View getSelectedTab() { |
| 106 | if ((mSelected >= 0) && (mSelected < mContentView.getChildCount())) { |
| 107 | return mContentView.getChildAt(mSelected); |
| 108 | } else { |
| 109 | return null; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | void clearTabs() { |
| 114 | mContentView.removeAllViews(); |
| 115 | } |
| 116 | |
| 117 | void addTab(View tab) { |
| 118 | mContentView.addView(tab); |
Michael Kolb | b7b115e | 2010-09-25 16:59:37 -0700 | [diff] [blame] | 119 | tab.setActivated(false); |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 122 | void removeTab(View tab) { |
Michael Kolb | bdc2a24 | 2010-09-07 13:48:07 -0700 | [diff] [blame] | 123 | int ix = mContentView.indexOfChild(tab); |
| 124 | if (ix == mSelected) { |
| 125 | mSelected = -1; |
| 126 | } else if (ix < mSelected) { |
| 127 | mSelected--; |
| 128 | } |
John Reck | 599e910 | 2011-01-06 20:37:31 -0800 | [diff] [blame] | 129 | mContentView.removeView(tab); |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Michael Kolb | 27b0839 | 2010-10-04 12:49:23 -0700 | [diff] [blame] | 132 | private void ensureChildVisible(View child) { |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 133 | if (child != null) { |
| 134 | int childl = child.getLeft(); |
| 135 | int childr = childl + child.getWidth(); |
| 136 | int viewl = getScrollX(); |
| 137 | int viewr = viewl + getWidth(); |
| 138 | if (childl < viewl) { |
| 139 | // need scrolling to left |
Michael Kolb | 27b0839 | 2010-10-04 12:49:23 -0700 | [diff] [blame] | 140 | animateScroll(childl); |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 141 | } else if (childr > viewr) { |
| 142 | // need scrolling to right |
Michael Kolb | 27b0839 | 2010-10-04 12:49:23 -0700 | [diff] [blame] | 143 | animateScroll(childr - viewr + viewl); |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | } |
| 147 | |
John Reck | 599e910 | 2011-01-06 20:37:31 -0800 | [diff] [blame] | 148 | // TODO: These animations are broken and don't work correctly, removing for now |
| 149 | // as animateOut is actually causing issues |
| 150 | // private void animateIn(View tab) { |
| 151 | // ObjectAnimator animator = ObjectAnimator.ofInt(tab, "TranslationX", 500, 0); |
| 152 | // animator.setDuration(mAnimationDuration); |
| 153 | // animator.start(); |
| 154 | // } |
| 155 | // |
| 156 | // private void animateOut(final View tab) { |
| 157 | // ObjectAnimator animator = ObjectAnimator.ofInt( |
| 158 | // tab, "TranslationX", 0, getScrollX() - tab.getRight()); |
| 159 | // animator.setDuration(mAnimationDuration); |
| 160 | // animator.addListener(new AnimatorListenerAdapter() { |
| 161 | // @Override |
| 162 | // public void onAnimationEnd(Animator animation) { |
| 163 | // mContentView.removeView(tab); |
| 164 | // } |
| 165 | // }); |
| 166 | // animator.setInterpolator(new AccelerateInterpolator()); |
| 167 | // animator.start(); |
| 168 | // } |
Michael Kolb | 27b0839 | 2010-10-04 12:49:23 -0700 | [diff] [blame] | 169 | |
| 170 | private void animateScroll(int newscroll) { |
Chet Haase | b3a00ab | 2010-10-14 07:01:46 -0700 | [diff] [blame] | 171 | ObjectAnimator animator = ObjectAnimator.ofInt(this, "scroll", getScrollX(), newscroll); |
| 172 | animator.setDuration(mAnimationDuration); |
Michael Kolb | 27b0839 | 2010-10-04 12:49:23 -0700 | [diff] [blame] | 173 | animator.start(); |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * required for animation |
| 178 | */ |
| 179 | public void setScroll(int newscroll) { |
| 180 | scrollTo(newscroll, getScrollY()); |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * required for animation |
| 185 | */ |
| 186 | public int getScroll() { |
| 187 | return getScrollX(); |
| 188 | } |
| 189 | |
Romain Guy | e398956 | 2011-01-27 12:06:29 -0800 | [diff] [blame^] | 190 | @Override |
| 191 | protected void onScrollChanged(int l, int t, int oldl, int oldt) { |
| 192 | super.onScrollChanged(l, t, oldl, oldt); |
| 193 | |
| 194 | // TabViews base their drawing based on their absolute position within the |
| 195 | // window. When hardware accelerated, we need to recreate their display list |
| 196 | // when they scroll |
| 197 | if (isHardwareAccelerated()) { |
| 198 | int count = mContentView.getChildCount(); |
| 199 | for (int i = 0; i < count; i++) { |
| 200 | mContentView.getChildAt(i).invalidate(); |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 | |
Michael Kolb | 2b5a13a | 2010-12-09 14:13:42 -0800 | [diff] [blame] | 205 | class TabLayout extends LinearLayout { |
| 206 | |
| 207 | public TabLayout(Context context) { |
| 208 | super(context); |
| 209 | setChildrenDrawingOrderEnabled(true); |
| 210 | } |
| 211 | |
| 212 | @Override |
| 213 | protected void onMeasure(int hspec, int vspec) { |
| 214 | super.onMeasure(hspec, vspec); |
| 215 | int w = getMeasuredWidth(); |
| 216 | w -= Math.max(0, mContentView.getChildCount() - 1) * mTabOverlap; |
| 217 | setMeasuredDimension(w, getMeasuredHeight()); |
| 218 | } |
| 219 | |
| 220 | @Override |
| 221 | protected void onLayout(boolean changed, int left, int top, int right, int bottom) { |
| 222 | super.onLayout(changed, left, top, right, bottom); |
| 223 | if (getChildCount() > 1) { |
| 224 | int nextLeft = getChildAt(0).getRight() - mTabOverlap; |
| 225 | for (int i = 1; i < getChildCount(); i++) { |
| 226 | View tab = getChildAt(i); |
| 227 | int w = tab.getRight() - tab.getLeft(); |
| 228 | tab.layout(nextLeft, tab.getTop(), nextLeft + w, tab.getBottom()); |
| 229 | nextLeft += w - mTabOverlap; |
| 230 | } |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | @Override |
| 235 | protected int getChildDrawingOrder(int count, int i) { |
| 236 | int next = -1; |
| 237 | if ((i == (count - 1)) && (mSelected >= 0)) { |
| 238 | next = mSelected; |
| 239 | } else { |
| 240 | next = count - i - 1; |
| 241 | if (next <= mSelected) { |
| 242 | next--; |
| 243 | } |
| 244 | } |
| 245 | return next; |
| 246 | } |
| 247 | |
| 248 | } |
| 249 | |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 250 | } |