blob: 435362dbfa22fca0bd69abea46e0e3c62e1c5415 [file] [log] [blame]
Michael Kolbfe251992010-07-08 15:41:55 -07001/*
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
17package com.android.browser;
18
19import android.content.Context;
20import android.util.AttributeSet;
21import android.view.View;
22import android.widget.HorizontalScrollView;
23import android.widget.LinearLayout;
24
25/**
26 * custom view for displaying tabs in the tabbed title bar
27 */
28public 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 Kolba2b2ba82010-08-04 17:54:03 -070069 LayoutParams.MATCH_PARENT));
Michael Kolbfe251992010-07-08 15:41:55 -070070 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) {
Michael Kolbb7b115e2010-09-25 16:59:37 -070083 v.setActivated(false);
Michael Kolbfe251992010-07-08 15:41:55 -070084 }
85 mSelected = position;
86 v = getSelectedTab();
87 if (v != null) {
Michael Kolbb7b115e2010-09-25 16:59:37 -070088 v.setActivated(true);
Michael Kolbfe251992010-07-08 15:41:55 -070089 }
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);
Michael Kolbb7b115e2010-09-25 16:59:37 -0700107 tab.setActivated(false);
Michael Kolbfe251992010-07-08 15:41:55 -0700108 }
109
110 void addTab(View tab, int pos) {
111 mContentView.addView(tab, pos);
Michael Kolbb7b115e2010-09-25 16:59:37 -0700112 tab.setActivated(false);
Michael Kolbfe251992010-07-08 15:41:55 -0700113 }
114
115 void removeTab(View tab) {
Michael Kolbbdc2a242010-09-07 13:48:07 -0700116 int ix = mContentView.indexOfChild(tab);
117 if (ix == mSelected) {
118 mSelected = -1;
119 } else if (ix < mSelected) {
120 mSelected--;
121 }
Michael Kolbfe251992010-07-08 15:41:55 -0700122 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}