blob: fcd5a80ba7e9865b9cec9b09cb4a9820ce9b8a57 [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
Michael Kolbebba8b42010-09-30 12:57:59 -070030 private BrowserActivity mBrowserActivity;
Michael Kolbfe251992010-07-08 15:41:55 -070031 private LinearLayout mContentView;
Michael Kolbfe251992010-07-08 15:41:55 -070032 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 Kolbebba8b42010-09-30 12:57:59 -070062 mBrowserActivity = (BrowserActivity)ctx;
Michael Kolbfe251992010-07-08 15:41:55 -070063 setHorizontalScrollBarEnabled(false);
Michael Kolbebba8b42010-09-30 12:57:59 -070064 mContentView = new LinearLayout(mBrowserActivity);
Michael Kolbfe251992010-07-08 15:41:55 -070065 mContentView.setOrientation(LinearLayout.HORIZONTAL);
66 mContentView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
Michael Kolba2b2ba82010-08-04 17:54:03 -070067 LayoutParams.MATCH_PARENT));
Michael Kolbfe251992010-07-08 15:41:55 -070068 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 Kolbb7b115e2010-09-25 16:59:37 -070081 v.setActivated(false);
Michael Kolbfe251992010-07-08 15:41:55 -070082 }
83 mSelected = position;
84 v = getSelectedTab();
85 if (v != null) {
Michael Kolbb7b115e2010-09-25 16:59:37 -070086 v.setActivated(true);
Michael Kolbfe251992010-07-08 15:41:55 -070087 }
88 requestLayout();
89 }
90
Michael Kolbebba8b42010-09-30 12:57:59 -070091 int getChildIndex(View v) {
92 return mContentView.indexOfChild(v);
93 }
94
Michael Kolbfe251992010-07-08 15:41:55 -070095 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 Kolbb7b115e2010-09-25 16:59:37 -0700109 tab.setActivated(false);
Michael Kolbfe251992010-07-08 15:41:55 -0700110 }
111
Michael Kolbfe251992010-07-08 15:41:55 -0700112 void removeTab(View tab) {
Michael Kolbbdc2a242010-09-07 13:48:07 -0700113 int ix = mContentView.indexOfChild(tab);
114 if (ix == mSelected) {
115 mSelected = -1;
116 } else if (ix < mSelected) {
117 mSelected--;
118 }
Michael Kolbfe251992010-07-08 15:41:55 -0700119 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 Kolbfe251992010-07-08 15:41:55 -0700138}