Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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 com.android.browser; |
| 18 | |
| 19 | import android.content.Context; |
Michael Kolb | c350376 | 2010-10-03 14:45:11 -0700 | [diff] [blame^] | 20 | import android.graphics.Canvas; |
| 21 | import android.graphics.drawable.Drawable; |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 22 | import android.util.AttributeSet; |
| 23 | import android.view.View; |
| 24 | import android.widget.HorizontalScrollView; |
| 25 | import android.widget.LinearLayout; |
| 26 | |
| 27 | /** |
| 28 | * custom view for displaying tabs in the tabbed title bar |
| 29 | */ |
| 30 | public class TabScrollView extends HorizontalScrollView { |
| 31 | |
Michael Kolb | ebba8b4 | 2010-09-30 12:57:59 -0700 | [diff] [blame] | 32 | private BrowserActivity mBrowserActivity; |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 33 | private LinearLayout mContentView; |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 34 | private int mSelected; |
Michael Kolb | c350376 | 2010-10-03 14:45:11 -0700 | [diff] [blame^] | 35 | private Drawable mArrowLeft; |
| 36 | private Drawable mArrowRight; |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 37 | |
| 38 | /** |
| 39 | * @param context |
| 40 | * @param attrs |
| 41 | * @param defStyle |
| 42 | */ |
| 43 | public TabScrollView(Context context, AttributeSet attrs, int defStyle) { |
| 44 | super(context, attrs, defStyle); |
| 45 | init(context); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * @param context |
| 50 | * @param attrs |
| 51 | */ |
| 52 | public TabScrollView(Context context, AttributeSet attrs) { |
| 53 | super(context, attrs); |
| 54 | init(context); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * @param context |
| 59 | */ |
| 60 | public TabScrollView(Context context) { |
| 61 | super(context); |
| 62 | init(context); |
| 63 | } |
| 64 | |
| 65 | private void init(Context ctx) { |
Michael Kolb | ebba8b4 | 2010-09-30 12:57:59 -0700 | [diff] [blame] | 66 | mBrowserActivity = (BrowserActivity)ctx; |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 67 | setHorizontalScrollBarEnabled(false); |
Michael Kolb | ebba8b4 | 2010-09-30 12:57:59 -0700 | [diff] [blame] | 68 | mContentView = new LinearLayout(mBrowserActivity); |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 69 | mContentView.setOrientation(LinearLayout.HORIZONTAL); |
| 70 | mContentView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 71 | LayoutParams.MATCH_PARENT)); |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 72 | addView(mContentView); |
| 73 | mSelected = -1; |
Michael Kolb | c350376 | 2010-10-03 14:45:11 -0700 | [diff] [blame^] | 74 | mArrowLeft = ctx.getResources().getDrawable(R.drawable.ic_arrow_left); |
| 75 | mArrowRight = ctx.getResources().getDrawable(R.drawable.ic_arrow_right); |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | @Override |
| 79 | protected void onLayout(boolean changed, int left, int top, int right, int bottom) { |
| 80 | super.onLayout(changed, left, top, right, bottom); |
| 81 | ensureChildVisible(getSelectedTab()); |
| 82 | } |
| 83 | |
| 84 | void setSelectedTab(int position) { |
| 85 | View v = getSelectedTab(); |
| 86 | if (v != null) { |
Michael Kolb | b7b115e | 2010-09-25 16:59:37 -0700 | [diff] [blame] | 87 | v.setActivated(false); |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 88 | } |
| 89 | mSelected = position; |
| 90 | v = getSelectedTab(); |
| 91 | if (v != null) { |
Michael Kolb | b7b115e | 2010-09-25 16:59:37 -0700 | [diff] [blame] | 92 | v.setActivated(true); |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 93 | } |
| 94 | requestLayout(); |
| 95 | } |
| 96 | |
Michael Kolb | ebba8b4 | 2010-09-30 12:57:59 -0700 | [diff] [blame] | 97 | int getChildIndex(View v) { |
| 98 | return mContentView.indexOfChild(v); |
| 99 | } |
| 100 | |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 101 | View getSelectedTab() { |
| 102 | if ((mSelected >= 0) && (mSelected < mContentView.getChildCount())) { |
| 103 | return mContentView.getChildAt(mSelected); |
| 104 | } else { |
| 105 | return null; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | void clearTabs() { |
| 110 | mContentView.removeAllViews(); |
| 111 | } |
| 112 | |
| 113 | void addTab(View tab) { |
| 114 | mContentView.addView(tab); |
Michael Kolb | b7b115e | 2010-09-25 16:59:37 -0700 | [diff] [blame] | 115 | tab.setActivated(false); |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 118 | void removeTab(View tab) { |
Michael Kolb | bdc2a24 | 2010-09-07 13:48:07 -0700 | [diff] [blame] | 119 | int ix = mContentView.indexOfChild(tab); |
| 120 | if (ix == mSelected) { |
| 121 | mSelected = -1; |
| 122 | } else if (ix < mSelected) { |
| 123 | mSelected--; |
| 124 | } |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 125 | mContentView.removeView(tab); |
| 126 | } |
| 127 | |
| 128 | void ensureChildVisible(View child) { |
| 129 | if (child != null) { |
| 130 | int childl = child.getLeft(); |
| 131 | int childr = childl + child.getWidth(); |
| 132 | int viewl = getScrollX(); |
| 133 | int viewr = viewl + getWidth(); |
| 134 | if (childl < viewl) { |
| 135 | // need scrolling to left |
| 136 | scrollTo(childl, 0); |
| 137 | } else if (childr > viewr) { |
| 138 | // need scrolling to right |
| 139 | scrollTo(childr - viewr + viewl, 0); |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | |
Michael Kolb | c350376 | 2010-10-03 14:45:11 -0700 | [diff] [blame^] | 144 | @Override |
| 145 | protected void dispatchDraw(Canvas canvas) { |
| 146 | super.dispatchDraw(canvas); |
| 147 | int l = getScrollX(); |
| 148 | int r = l + getWidth(); |
| 149 | int dis = 8; |
| 150 | if (l > 0) { |
| 151 | int aw = mArrowLeft.getIntrinsicWidth(); |
| 152 | mArrowLeft.setBounds(l + dis, 0, l + dis + aw, getHeight()); |
| 153 | mArrowLeft.draw(canvas); |
| 154 | } |
| 155 | if (r < mContentView.getWidth()) { |
| 156 | int aw = mArrowRight.getIntrinsicWidth(); |
| 157 | mArrowRight.setBounds(r - dis - aw, 0, r - dis, getHeight()); |
| 158 | mArrowRight.draw(canvas); |
| 159 | } |
| 160 | } |
| 161 | |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 162 | } |