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 | |
| 30 | private Context mContext; |
| 31 | |
| 32 | private LinearLayout mContentView; |
| 33 | |
| 34 | private int mSelected; |
| 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) { |
| 64 | mContext = ctx; |
| 65 | setHorizontalScrollBarEnabled(false); |
| 66 | mContentView = new LinearLayout(mContext); |
| 67 | mContentView.setOrientation(LinearLayout.HORIZONTAL); |
| 68 | mContentView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 69 | LayoutParams.MATCH_PARENT)); |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 70 | addView(mContentView); |
| 71 | mSelected = -1; |
| 72 | } |
| 73 | |
| 74 | @Override |
| 75 | protected void onLayout(boolean changed, int left, int top, int right, int bottom) { |
| 76 | super.onLayout(changed, left, top, right, bottom); |
| 77 | ensureChildVisible(getSelectedTab()); |
| 78 | } |
| 79 | |
| 80 | void setSelectedTab(int position) { |
| 81 | View v = getSelectedTab(); |
| 82 | if (v != null) { |
| 83 | v.setSelected(false); |
| 84 | } |
| 85 | mSelected = position; |
| 86 | v = getSelectedTab(); |
| 87 | if (v != null) { |
| 88 | v.setSelected(true); |
| 89 | } |
| 90 | requestLayout(); |
| 91 | } |
| 92 | |
| 93 | View getSelectedTab() { |
| 94 | if ((mSelected >= 0) && (mSelected < mContentView.getChildCount())) { |
| 95 | return mContentView.getChildAt(mSelected); |
| 96 | } else { |
| 97 | return null; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | void clearTabs() { |
| 102 | mContentView.removeAllViews(); |
| 103 | } |
| 104 | |
| 105 | void addTab(View tab) { |
| 106 | mContentView.addView(tab); |
| 107 | tab.setSelected(false); |
| 108 | } |
| 109 | |
| 110 | void addTab(View tab, int pos) { |
| 111 | mContentView.addView(tab, pos); |
| 112 | tab.setSelected(false); |
| 113 | } |
| 114 | |
| 115 | void removeTab(View tab) { |
Michael Kolb | bdc2a24 | 2010-09-07 13:48:07 -0700 | [diff] [blame^] | 116 | int ix = mContentView.indexOfChild(tab); |
| 117 | if (ix == mSelected) { |
| 118 | mSelected = -1; |
| 119 | } else if (ix < mSelected) { |
| 120 | mSelected--; |
| 121 | } |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 122 | mContentView.removeView(tab); |
| 123 | } |
| 124 | |
| 125 | void ensureChildVisible(View child) { |
| 126 | if (child != null) { |
| 127 | int childl = child.getLeft(); |
| 128 | int childr = childl + child.getWidth(); |
| 129 | int viewl = getScrollX(); |
| 130 | int viewr = viewl + getWidth(); |
| 131 | if (childl < viewl) { |
| 132 | // need scrolling to left |
| 133 | scrollTo(childl, 0); |
| 134 | } else if (childr > viewr) { |
| 135 | // need scrolling to right |
| 136 | scrollTo(childr - viewr + viewl, 0); |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | |
| 142 | } |