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