blob: dc21cb6bed6c46f474c17cc7940c54d601afd690 [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;
Michael Kolbc3503762010-10-03 14:45:11 -070020import android.graphics.Canvas;
21import android.graphics.drawable.Drawable;
Michael Kolbfe251992010-07-08 15:41:55 -070022import android.util.AttributeSet;
23import android.view.View;
24import android.widget.HorizontalScrollView;
25import android.widget.LinearLayout;
26
27/**
28 * custom view for displaying tabs in the tabbed title bar
29 */
30public class TabScrollView extends HorizontalScrollView {
31
Michael Kolbebba8b42010-09-30 12:57:59 -070032 private BrowserActivity mBrowserActivity;
Michael Kolbfe251992010-07-08 15:41:55 -070033 private LinearLayout mContentView;
Michael Kolbfe251992010-07-08 15:41:55 -070034 private int mSelected;
Michael Kolbc3503762010-10-03 14:45:11 -070035 private Drawable mArrowLeft;
36 private Drawable mArrowRight;
Michael Kolbfe251992010-07-08 15:41:55 -070037
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 Kolbebba8b42010-09-30 12:57:59 -070066 mBrowserActivity = (BrowserActivity)ctx;
Michael Kolbfe251992010-07-08 15:41:55 -070067 setHorizontalScrollBarEnabled(false);
Michael Kolbebba8b42010-09-30 12:57:59 -070068 mContentView = new LinearLayout(mBrowserActivity);
Michael Kolbfe251992010-07-08 15:41:55 -070069 mContentView.setOrientation(LinearLayout.HORIZONTAL);
70 mContentView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
Michael Kolba2b2ba82010-08-04 17:54:03 -070071 LayoutParams.MATCH_PARENT));
Michael Kolbfe251992010-07-08 15:41:55 -070072 addView(mContentView);
73 mSelected = -1;
Michael Kolbc3503762010-10-03 14:45:11 -070074 mArrowLeft = ctx.getResources().getDrawable(R.drawable.ic_arrow_left);
75 mArrowRight = ctx.getResources().getDrawable(R.drawable.ic_arrow_right);
Michael Kolbfe251992010-07-08 15:41:55 -070076 }
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 Kolbb7b115e2010-09-25 16:59:37 -070087 v.setActivated(false);
Michael Kolbfe251992010-07-08 15:41:55 -070088 }
89 mSelected = position;
90 v = getSelectedTab();
91 if (v != null) {
Michael Kolbb7b115e2010-09-25 16:59:37 -070092 v.setActivated(true);
Michael Kolbfe251992010-07-08 15:41:55 -070093 }
94 requestLayout();
95 }
96
Michael Kolbebba8b42010-09-30 12:57:59 -070097 int getChildIndex(View v) {
98 return mContentView.indexOfChild(v);
99 }
100
Michael Kolbfe251992010-07-08 15:41:55 -0700101 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 Kolbb7b115e2010-09-25 16:59:37 -0700115 tab.setActivated(false);
Michael Kolbfe251992010-07-08 15:41:55 -0700116 }
117
Michael Kolbfe251992010-07-08 15:41:55 -0700118 void removeTab(View tab) {
Michael Kolbbdc2a242010-09-07 13:48:07 -0700119 int ix = mContentView.indexOfChild(tab);
120 if (ix == mSelected) {
121 mSelected = -1;
122 } else if (ix < mSelected) {
123 mSelected--;
124 }
Michael Kolbfe251992010-07-08 15:41:55 -0700125 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 Kolbc3503762010-10-03 14:45:11 -0700144 @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 Kolbfe251992010-07-08 15:41:55 -0700162}