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