TabScrollView.java revision 599e9105f538972be3e2eb96c651b9e12a3516cf
1fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb/*
2fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb * Copyright (C) 2010 The Android Open Source Project
3fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb *
427b08397a287c46b2121738f653bfef0c08e6f29Michael Kolb * Licensed under the Apache License, Version 2.0 (the "License"); you may not
527b08397a287c46b2121738f653bfef0c08e6f29Michael Kolb * use this file except in compliance with the License. You may obtain a copy of
627b08397a287c46b2121738f653bfef0c08e6f29Michael Kolb * the License at
7fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb *
827b08397a287c46b2121738f653bfef0c08e6f29Michael Kolb * http://www.apache.org/licenses/LICENSE-2.0
9fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb *
10fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb * Unless required by applicable law or agreed to in writing, software
1127b08397a287c46b2121738f653bfef0c08e6f29Michael Kolb * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
1227b08397a287c46b2121738f653bfef0c08e6f29Michael Kolb * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
1327b08397a287c46b2121738f653bfef0c08e6f29Michael Kolb * License for the specific language governing permissions and limitations under
1427b08397a287c46b2121738f653bfef0c08e6f29Michael Kolb * the License.
15fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb */
16fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb
17fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolbpackage com.android.browser;
18fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb
1927b08397a287c46b2121738f653bfef0c08e6f29Michael Kolbimport android.animation.Animator;
20b3a00ab2336bdb2871924232b744160f47116a78Chet Haaseimport android.animation.AnimatorListenerAdapter;
2127b08397a287c46b2121738f653bfef0c08e6f29Michael Kolbimport android.animation.ObjectAnimator;
22fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolbimport android.content.Context;
23c350376ec7b7baa830aefb98fd5c8db1bf89c61fMichael Kolbimport android.graphics.Canvas;
24c350376ec7b7baa830aefb98fd5c8db1bf89c61fMichael Kolbimport android.graphics.drawable.Drawable;
25fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolbimport android.util.AttributeSet;
26fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolbimport android.view.View;
2727b08397a287c46b2121738f653bfef0c08e6f29Michael Kolbimport android.view.animation.AccelerateInterpolator;
28fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolbimport android.widget.HorizontalScrollView;
29fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolbimport android.widget.LinearLayout;
30fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb
31fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb/**
32fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb * custom view for displaying tabs in the tabbed title bar
33fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb */
34fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolbpublic class TabScrollView extends HorizontalScrollView {
35fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb
368233facddcc51865d612a919d450db6954aa48e3Michael Kolb    private Context mContext;
37fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb    private LinearLayout mContentView;
38fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb    private int mSelected;
39c350376ec7b7baa830aefb98fd5c8db1bf89c61fMichael Kolb    private Drawable mArrowLeft;
40c350376ec7b7baa830aefb98fd5c8db1bf89c61fMichael Kolb    private Drawable mArrowRight;
4127b08397a287c46b2121738f653bfef0c08e6f29Michael Kolb    private int mAnimationDuration;
422b5a13ab55991f55ab7afb107e4401c6fbb5ad64Michael Kolb    private int mTabOverlap;
43fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb
44fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb    /**
45fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb     * @param context
46fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb     * @param attrs
47fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb     * @param defStyle
48fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb     */
49fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb    public TabScrollView(Context context, AttributeSet attrs, int defStyle) {
50fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb        super(context, attrs, defStyle);
51fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb        init(context);
52fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb    }
53fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb
54fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb    /**
55fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb     * @param context
56fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb     * @param attrs
57fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb     */
58fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb    public TabScrollView(Context context, AttributeSet attrs) {
59fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb        super(context, attrs);
60fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb        init(context);
61fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb    }
62fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb
63fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb    /**
64fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb     * @param context
65fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb     */
66fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb    public TabScrollView(Context context) {
67fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb        super(context);
68fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb        init(context);
69fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb    }
70fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb
71fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb    private void init(Context ctx) {
728233facddcc51865d612a919d450db6954aa48e3Michael Kolb        mContext = ctx;
7327b08397a287c46b2121738f653bfef0c08e6f29Michael Kolb        mAnimationDuration = ctx.getResources().getInteger(
7427b08397a287c46b2121738f653bfef0c08e6f29Michael Kolb                R.integer.tab_animation_duration);
752b5a13ab55991f55ab7afb107e4401c6fbb5ad64Michael Kolb        mTabOverlap = (int) ctx.getResources().getDimension(R.dimen.tab_overlap);
76fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb        setHorizontalScrollBarEnabled(false);
772b5a13ab55991f55ab7afb107e4401c6fbb5ad64Michael Kolb        setOverScrollMode(OVER_SCROLL_NEVER);
782b5a13ab55991f55ab7afb107e4401c6fbb5ad64Michael Kolb        mContentView = new TabLayout(mContext);
79fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb        mContentView.setOrientation(LinearLayout.HORIZONTAL);
8027b08397a287c46b2121738f653bfef0c08e6f29Michael Kolb        mContentView.setLayoutParams(
8127b08397a287c46b2121738f653bfef0c08e6f29Michael Kolb                new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT));
82fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb        addView(mContentView);
83fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb        mSelected = -1;
84c350376ec7b7baa830aefb98fd5c8db1bf89c61fMichael Kolb        mArrowLeft = ctx.getResources().getDrawable(R.drawable.ic_arrow_left);
85c350376ec7b7baa830aefb98fd5c8db1bf89c61fMichael Kolb        mArrowRight = ctx.getResources().getDrawable(R.drawable.ic_arrow_right);
8627b08397a287c46b2121738f653bfef0c08e6f29Michael Kolb        // prevent ProGuard from removing the property methods
8727b08397a287c46b2121738f653bfef0c08e6f29Michael Kolb        setScroll(getScroll());
88fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb    }
89fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb
90fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb    @Override
91fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
92fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb        super.onLayout(changed, left, top, right, bottom);
93fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb        ensureChildVisible(getSelectedTab());
94fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb    }
95fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb
96fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb    void setSelectedTab(int position) {
97fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb        View v = getSelectedTab();
98fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb        if (v != null) {
99b7b115e301334f813275aa13e7a2cc27bf15fe6aMichael Kolb            v.setActivated(false);
100fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb        }
101fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb        mSelected = position;
102fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb        v = getSelectedTab();
103fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb        if (v != null) {
104b7b115e301334f813275aa13e7a2cc27bf15fe6aMichael Kolb            v.setActivated(true);
105fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb        }
106fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb        requestLayout();
107fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb    }
108fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb
109ebba8b4e9dc4f78764d6cfb26738b54d04bc9d27Michael Kolb    int getChildIndex(View v) {
110ebba8b4e9dc4f78764d6cfb26738b54d04bc9d27Michael Kolb        return mContentView.indexOfChild(v);
111ebba8b4e9dc4f78764d6cfb26738b54d04bc9d27Michael Kolb    }
112ebba8b4e9dc4f78764d6cfb26738b54d04bc9d27Michael Kolb
113fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb    View getSelectedTab() {
114fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb        if ((mSelected >= 0) && (mSelected < mContentView.getChildCount())) {
115fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb            return mContentView.getChildAt(mSelected);
116fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb        } else {
117fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb            return null;
118fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb        }
119fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb    }
120fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb
121fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb    void clearTabs() {
122fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb        mContentView.removeAllViews();
123fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb    }
124fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb
125fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb    void addTab(View tab) {
126fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb        mContentView.addView(tab);
127b7b115e301334f813275aa13e7a2cc27bf15fe6aMichael Kolb        tab.setActivated(false);
128fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb    }
129fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb
130fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb    void removeTab(View tab) {
131bdc2a2420281c96a09bd00136cf9b1244b35ed2bMichael Kolb        int ix = mContentView.indexOfChild(tab);
132bdc2a2420281c96a09bd00136cf9b1244b35ed2bMichael Kolb        if (ix == mSelected) {
133bdc2a2420281c96a09bd00136cf9b1244b35ed2bMichael Kolb            mSelected = -1;
134bdc2a2420281c96a09bd00136cf9b1244b35ed2bMichael Kolb        } else if (ix < mSelected) {
135bdc2a2420281c96a09bd00136cf9b1244b35ed2bMichael Kolb            mSelected--;
136bdc2a2420281c96a09bd00136cf9b1244b35ed2bMichael Kolb        }
137599e9105f538972be3e2eb96c651b9e12a3516cfJohn Reck        mContentView.removeView(tab);
138fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb    }
139fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb
14027b08397a287c46b2121738f653bfef0c08e6f29Michael Kolb    private void ensureChildVisible(View child) {
141fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb        if (child != null) {
142fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb            int childl = child.getLeft();
143fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb            int childr = childl + child.getWidth();
144fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb            int viewl = getScrollX();
145fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb            int viewr = viewl + getWidth();
146fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb            if (childl < viewl) {
147fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb                // need scrolling to left
14827b08397a287c46b2121738f653bfef0c08e6f29Michael Kolb                animateScroll(childl);
149fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb            } else if (childr > viewr) {
150fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb                // need scrolling to right
15127b08397a287c46b2121738f653bfef0c08e6f29Michael Kolb                animateScroll(childr - viewr + viewl);
152fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb            }
153fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb        }
154fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb    }
155fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb
156c350376ec7b7baa830aefb98fd5c8db1bf89c61fMichael Kolb    @Override
157c350376ec7b7baa830aefb98fd5c8db1bf89c61fMichael Kolb    protected void dispatchDraw(Canvas canvas) {
158c350376ec7b7baa830aefb98fd5c8db1bf89c61fMichael Kolb        super.dispatchDraw(canvas);
159c350376ec7b7baa830aefb98fd5c8db1bf89c61fMichael Kolb        int l = getScrollX();
160c350376ec7b7baa830aefb98fd5c8db1bf89c61fMichael Kolb        int r = l + getWidth();
161c350376ec7b7baa830aefb98fd5c8db1bf89c61fMichael Kolb        int dis = 8;
162c350376ec7b7baa830aefb98fd5c8db1bf89c61fMichael Kolb        if (l > 0) {
163c350376ec7b7baa830aefb98fd5c8db1bf89c61fMichael Kolb            int aw = mArrowLeft.getIntrinsicWidth();
164c350376ec7b7baa830aefb98fd5c8db1bf89c61fMichael Kolb            mArrowLeft.setBounds(l + dis, 0, l + dis + aw, getHeight());
165c350376ec7b7baa830aefb98fd5c8db1bf89c61fMichael Kolb            mArrowLeft.draw(canvas);
166c350376ec7b7baa830aefb98fd5c8db1bf89c61fMichael Kolb        }
167c350376ec7b7baa830aefb98fd5c8db1bf89c61fMichael Kolb        if (r < mContentView.getWidth()) {
168c350376ec7b7baa830aefb98fd5c8db1bf89c61fMichael Kolb            int aw = mArrowRight.getIntrinsicWidth();
169c350376ec7b7baa830aefb98fd5c8db1bf89c61fMichael Kolb            mArrowRight.setBounds(r - dis - aw, 0, r - dis, getHeight());
170c350376ec7b7baa830aefb98fd5c8db1bf89c61fMichael Kolb            mArrowRight.draw(canvas);
171c350376ec7b7baa830aefb98fd5c8db1bf89c61fMichael Kolb        }
172c350376ec7b7baa830aefb98fd5c8db1bf89c61fMichael Kolb    }
173c350376ec7b7baa830aefb98fd5c8db1bf89c61fMichael Kolb
174599e9105f538972be3e2eb96c651b9e12a3516cfJohn Reck// TODO: These animations are broken and don't work correctly, removing for now
175599e9105f538972be3e2eb96c651b9e12a3516cfJohn Reck//       as animateOut is actually causing issues
176599e9105f538972be3e2eb96c651b9e12a3516cfJohn Reck//    private void animateIn(View tab) {
177599e9105f538972be3e2eb96c651b9e12a3516cfJohn Reck//        ObjectAnimator animator = ObjectAnimator.ofInt(tab, "TranslationX", 500, 0);
178599e9105f538972be3e2eb96c651b9e12a3516cfJohn Reck//        animator.setDuration(mAnimationDuration);
179599e9105f538972be3e2eb96c651b9e12a3516cfJohn Reck//        animator.start();
180599e9105f538972be3e2eb96c651b9e12a3516cfJohn Reck//    }
181599e9105f538972be3e2eb96c651b9e12a3516cfJohn Reck//
182599e9105f538972be3e2eb96c651b9e12a3516cfJohn Reck//    private void animateOut(final View tab) {
183599e9105f538972be3e2eb96c651b9e12a3516cfJohn Reck//        ObjectAnimator animator = ObjectAnimator.ofInt(
184599e9105f538972be3e2eb96c651b9e12a3516cfJohn Reck//                tab, "TranslationX", 0, getScrollX() - tab.getRight());
185599e9105f538972be3e2eb96c651b9e12a3516cfJohn Reck//        animator.setDuration(mAnimationDuration);
186599e9105f538972be3e2eb96c651b9e12a3516cfJohn Reck//        animator.addListener(new AnimatorListenerAdapter() {
187599e9105f538972be3e2eb96c651b9e12a3516cfJohn Reck//            @Override
188599e9105f538972be3e2eb96c651b9e12a3516cfJohn Reck//            public void onAnimationEnd(Animator animation) {
189599e9105f538972be3e2eb96c651b9e12a3516cfJohn Reck//                mContentView.removeView(tab);
190599e9105f538972be3e2eb96c651b9e12a3516cfJohn Reck//            }
191599e9105f538972be3e2eb96c651b9e12a3516cfJohn Reck//        });
192599e9105f538972be3e2eb96c651b9e12a3516cfJohn Reck//        animator.setInterpolator(new AccelerateInterpolator());
193599e9105f538972be3e2eb96c651b9e12a3516cfJohn Reck//        animator.start();
194599e9105f538972be3e2eb96c651b9e12a3516cfJohn Reck//    }
19527b08397a287c46b2121738f653bfef0c08e6f29Michael Kolb
19627b08397a287c46b2121738f653bfef0c08e6f29Michael Kolb    private void animateScroll(int newscroll) {
197b3a00ab2336bdb2871924232b744160f47116a78Chet Haase        ObjectAnimator animator = ObjectAnimator.ofInt(this, "scroll", getScrollX(), newscroll);
198b3a00ab2336bdb2871924232b744160f47116a78Chet Haase        animator.setDuration(mAnimationDuration);
19927b08397a287c46b2121738f653bfef0c08e6f29Michael Kolb        animator.start();
20027b08397a287c46b2121738f653bfef0c08e6f29Michael Kolb    }
20127b08397a287c46b2121738f653bfef0c08e6f29Michael Kolb
20227b08397a287c46b2121738f653bfef0c08e6f29Michael Kolb    /**
20327b08397a287c46b2121738f653bfef0c08e6f29Michael Kolb     * required for animation
20427b08397a287c46b2121738f653bfef0c08e6f29Michael Kolb     */
20527b08397a287c46b2121738f653bfef0c08e6f29Michael Kolb    public void setScroll(int newscroll) {
20627b08397a287c46b2121738f653bfef0c08e6f29Michael Kolb        scrollTo(newscroll, getScrollY());
20727b08397a287c46b2121738f653bfef0c08e6f29Michael Kolb    }
20827b08397a287c46b2121738f653bfef0c08e6f29Michael Kolb
20927b08397a287c46b2121738f653bfef0c08e6f29Michael Kolb    /**
21027b08397a287c46b2121738f653bfef0c08e6f29Michael Kolb     * required for animation
21127b08397a287c46b2121738f653bfef0c08e6f29Michael Kolb     */
21227b08397a287c46b2121738f653bfef0c08e6f29Michael Kolb    public int getScroll() {
21327b08397a287c46b2121738f653bfef0c08e6f29Michael Kolb        return getScrollX();
21427b08397a287c46b2121738f653bfef0c08e6f29Michael Kolb    }
21527b08397a287c46b2121738f653bfef0c08e6f29Michael Kolb
2162b5a13ab55991f55ab7afb107e4401c6fbb5ad64Michael Kolb    class TabLayout extends LinearLayout {
2172b5a13ab55991f55ab7afb107e4401c6fbb5ad64Michael Kolb
2182b5a13ab55991f55ab7afb107e4401c6fbb5ad64Michael Kolb        public TabLayout(Context context) {
2192b5a13ab55991f55ab7afb107e4401c6fbb5ad64Michael Kolb            super(context);
2202b5a13ab55991f55ab7afb107e4401c6fbb5ad64Michael Kolb            setChildrenDrawingOrderEnabled(true);
2212b5a13ab55991f55ab7afb107e4401c6fbb5ad64Michael Kolb        }
2222b5a13ab55991f55ab7afb107e4401c6fbb5ad64Michael Kolb
2232b5a13ab55991f55ab7afb107e4401c6fbb5ad64Michael Kolb        @Override
2242b5a13ab55991f55ab7afb107e4401c6fbb5ad64Michael Kolb        protected void onMeasure(int hspec, int vspec) {
2252b5a13ab55991f55ab7afb107e4401c6fbb5ad64Michael Kolb            super.onMeasure(hspec, vspec);
2262b5a13ab55991f55ab7afb107e4401c6fbb5ad64Michael Kolb            int w = getMeasuredWidth();
2272b5a13ab55991f55ab7afb107e4401c6fbb5ad64Michael Kolb            w -= Math.max(0, mContentView.getChildCount() - 1) * mTabOverlap;
2282b5a13ab55991f55ab7afb107e4401c6fbb5ad64Michael Kolb            setMeasuredDimension(w, getMeasuredHeight());
2292b5a13ab55991f55ab7afb107e4401c6fbb5ad64Michael Kolb        }
2302b5a13ab55991f55ab7afb107e4401c6fbb5ad64Michael Kolb
2312b5a13ab55991f55ab7afb107e4401c6fbb5ad64Michael Kolb        @Override
2322b5a13ab55991f55ab7afb107e4401c6fbb5ad64Michael Kolb        protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
2332b5a13ab55991f55ab7afb107e4401c6fbb5ad64Michael Kolb            super.onLayout(changed, left, top, right, bottom);
2342b5a13ab55991f55ab7afb107e4401c6fbb5ad64Michael Kolb            if (getChildCount() > 1) {
2352b5a13ab55991f55ab7afb107e4401c6fbb5ad64Michael Kolb                int nextLeft = getChildAt(0).getRight() - mTabOverlap;
2362b5a13ab55991f55ab7afb107e4401c6fbb5ad64Michael Kolb                for (int i = 1; i < getChildCount(); i++) {
2372b5a13ab55991f55ab7afb107e4401c6fbb5ad64Michael Kolb                    View tab = getChildAt(i);
2382b5a13ab55991f55ab7afb107e4401c6fbb5ad64Michael Kolb                    int w = tab.getRight() - tab.getLeft();
2392b5a13ab55991f55ab7afb107e4401c6fbb5ad64Michael Kolb                    tab.layout(nextLeft, tab.getTop(), nextLeft + w, tab.getBottom());
2402b5a13ab55991f55ab7afb107e4401c6fbb5ad64Michael Kolb                    nextLeft += w - mTabOverlap;
2412b5a13ab55991f55ab7afb107e4401c6fbb5ad64Michael Kolb                }
2422b5a13ab55991f55ab7afb107e4401c6fbb5ad64Michael Kolb            }
2432b5a13ab55991f55ab7afb107e4401c6fbb5ad64Michael Kolb        }
2442b5a13ab55991f55ab7afb107e4401c6fbb5ad64Michael Kolb
2452b5a13ab55991f55ab7afb107e4401c6fbb5ad64Michael Kolb        @Override
2462b5a13ab55991f55ab7afb107e4401c6fbb5ad64Michael Kolb        protected int getChildDrawingOrder(int count, int i) {
2472b5a13ab55991f55ab7afb107e4401c6fbb5ad64Michael Kolb            int next = -1;
2482b5a13ab55991f55ab7afb107e4401c6fbb5ad64Michael Kolb            if ((i == (count - 1)) && (mSelected >= 0)) {
2492b5a13ab55991f55ab7afb107e4401c6fbb5ad64Michael Kolb                next = mSelected;
2502b5a13ab55991f55ab7afb107e4401c6fbb5ad64Michael Kolb            } else {
2512b5a13ab55991f55ab7afb107e4401c6fbb5ad64Michael Kolb                next = count - i - 1;
2522b5a13ab55991f55ab7afb107e4401c6fbb5ad64Michael Kolb                if (next <= mSelected) {
2532b5a13ab55991f55ab7afb107e4401c6fbb5ad64Michael Kolb                    next--;
2542b5a13ab55991f55ab7afb107e4401c6fbb5ad64Michael Kolb                }
2552b5a13ab55991f55ab7afb107e4401c6fbb5ad64Michael Kolb            }
2562b5a13ab55991f55ab7afb107e4401c6fbb5ad64Michael Kolb            return next;
2572b5a13ab55991f55ab7afb107e4401c6fbb5ad64Michael Kolb        }
2582b5a13ab55991f55ab7afb107e4401c6fbb5ad64Michael Kolb
2592b5a13ab55991f55ab7afb107e4401c6fbb5ad64Michael Kolb    }
2602b5a13ab55991f55ab7afb107e4401c6fbb5ad64Michael Kolb
261fe25199a6f975c67d28afcc1de56b0f987b66cd8Michael Kolb}
262