TabScrollView.java revision c350376ec7b7baa830aefb98fd5c8db1bf89c61f
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
17package com.android.browser;
18
19import android.content.Context;
20import android.graphics.Canvas;
21import android.graphics.drawable.Drawable;
22import 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
32    private BrowserActivity mBrowserActivity;
33    private LinearLayout mContentView;
34    private int mSelected;
35    private Drawable mArrowLeft;
36    private Drawable mArrowRight;
37
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) {
66        mBrowserActivity = (BrowserActivity)ctx;
67        setHorizontalScrollBarEnabled(false);
68        mContentView = new LinearLayout(mBrowserActivity);
69        mContentView.setOrientation(LinearLayout.HORIZONTAL);
70        mContentView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
71                LayoutParams.MATCH_PARENT));
72        addView(mContentView);
73        mSelected = -1;
74        mArrowLeft = ctx.getResources().getDrawable(R.drawable.ic_arrow_left);
75        mArrowRight = ctx.getResources().getDrawable(R.drawable.ic_arrow_right);
76    }
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) {
87            v.setActivated(false);
88        }
89        mSelected = position;
90        v = getSelectedTab();
91        if (v != null) {
92            v.setActivated(true);
93        }
94        requestLayout();
95    }
96
97    int getChildIndex(View v) {
98        return mContentView.indexOfChild(v);
99    }
100
101    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);
115        tab.setActivated(false);
116    }
117
118    void removeTab(View tab) {
119        int ix = mContentView.indexOfChild(tab);
120        if (ix == mSelected) {
121            mSelected = -1;
122        } else if (ix < mSelected) {
123            mSelected--;
124        }
125        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
144    @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
162}
163