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