mobile/android/base/home/TabMenuStrip.java

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*-
michael@0 2 * This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 package org.mozilla.gecko.home;
michael@0 7
michael@0 8 import org.mozilla.gecko.R;
michael@0 9
michael@0 10 import android.content.Context;
michael@0 11 import android.content.res.TypedArray;
michael@0 12 import android.graphics.Canvas;
michael@0 13 import android.graphics.drawable.Drawable;
michael@0 14 import android.util.AttributeSet;
michael@0 15 import android.view.LayoutInflater;
michael@0 16 import android.view.View;
michael@0 17 import android.view.ViewTreeObserver;
michael@0 18 import android.view.accessibility.AccessibilityEvent;
michael@0 19 import android.widget.HorizontalScrollView;
michael@0 20 import android.widget.LinearLayout;
michael@0 21 import android.widget.TextView;
michael@0 22
michael@0 23 /**
michael@0 24 * {@code TabMenuStrip} is the view used to display {@code HomePager} tabs
michael@0 25 * on tablets. See {@code TabMenuStripLayout} for details about how the
michael@0 26 * tabs are created and updated.
michael@0 27 */
michael@0 28 public class TabMenuStrip extends HorizontalScrollView
michael@0 29 implements HomePager.Decor {
michael@0 30
michael@0 31 // Offset between the selected tab title and the edge of the screen,
michael@0 32 // except for the first and last tab in the tab strip.
michael@0 33 private static final int TITLE_OFFSET_DIPS = 24;
michael@0 34
michael@0 35 private final int titleOffset;
michael@0 36 private final TabMenuStripLayout layout;
michael@0 37
michael@0 38 public TabMenuStrip(Context context, AttributeSet attrs) {
michael@0 39 super(context, attrs);
michael@0 40
michael@0 41 // Disable the scroll bar.
michael@0 42 setHorizontalScrollBarEnabled(false);
michael@0 43
michael@0 44 titleOffset = (int) (TITLE_OFFSET_DIPS * getResources().getDisplayMetrics().density);
michael@0 45
michael@0 46 layout = new TabMenuStripLayout(context, attrs);
michael@0 47 addView(layout, LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
michael@0 48 }
michael@0 49
michael@0 50 @Override
michael@0 51 public void onAddPagerView(String title) {
michael@0 52 layout.onAddPagerView(title);
michael@0 53 }
michael@0 54
michael@0 55 @Override
michael@0 56 public void removeAllPagerViews() {
michael@0 57 layout.removeAllViews();
michael@0 58 }
michael@0 59
michael@0 60 @Override
michael@0 61 public void onPageSelected(final int position) {
michael@0 62 layout.onPageSelected(position);
michael@0 63 }
michael@0 64
michael@0 65 @Override
michael@0 66 public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
michael@0 67 layout.onPageScrolled(position, positionOffset, positionOffsetPixels);
michael@0 68
michael@0 69 final View selectedTitle = layout.getChildAt(position);
michael@0 70 if (selectedTitle == null) {
michael@0 71 return;
michael@0 72 }
michael@0 73
michael@0 74 final int selectedTitleOffset = (int) (positionOffset * selectedTitle.getWidth());
michael@0 75
michael@0 76 int titleLeft = selectedTitle.getLeft() + selectedTitleOffset;
michael@0 77 if (position > 0) {
michael@0 78 titleLeft -= titleOffset;
michael@0 79 }
michael@0 80
michael@0 81 int titleRight = selectedTitle.getRight() + selectedTitleOffset;
michael@0 82 if (position < layout.getChildCount() - 1) {
michael@0 83 titleRight += titleOffset;
michael@0 84 }
michael@0 85
michael@0 86 final int scrollX = getScrollX();
michael@0 87 if (titleLeft < scrollX) {
michael@0 88 // Tab strip overflows to the left.
michael@0 89 scrollTo(titleLeft, 0);
michael@0 90 } else if (titleRight > scrollX + getWidth()) {
michael@0 91 // Tab strip overflows to the right.
michael@0 92 scrollTo(titleRight - getWidth(), 0);
michael@0 93 }
michael@0 94 }
michael@0 95
michael@0 96 @Override
michael@0 97 public void setOnTitleClickListener(HomePager.OnTitleClickListener onTitleClickListener) {
michael@0 98 layout.setOnTitleClickListener(onTitleClickListener);
michael@0 99 }
michael@0 100 }

mercurial