The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006 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 | |
| 17 | package android.widget; |
| 18 | |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 19 | import android.annotation.DrawableRes; |
Alan Viverette | 7eb09d1 | 2015-08-14 11:36:01 -0400 | [diff] [blame] | 20 | import android.annotation.Nullable; |
Artur Satayev | ed5a6ae | 2019-12-10 17:47:54 +0000 | [diff] [blame] | 21 | import android.compat.annotation.UnsupportedAppUsage; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | import android.content.Context; |
| 23 | import android.content.res.TypedArray; |
| 24 | import android.graphics.Canvas; |
| 25 | import android.graphics.Rect; |
| 26 | import android.graphics.drawable.Drawable; |
Mike Cleron | 7609764 | 2009-09-25 17:53:56 -0700 | [diff] [blame] | 27 | import android.os.Build; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | import android.util.AttributeSet; |
Aurimas Liutikas | 99441c5 | 2016-10-11 16:48:32 -0700 | [diff] [blame] | 29 | import android.view.MotionEvent; |
| 30 | import android.view.PointerIcon; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | import android.view.View; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | import android.view.View.OnFocusChangeListener; |
Gilles Debunne | 97dfd34 | 2010-10-20 16:31:15 -0700 | [diff] [blame] | 33 | import android.view.ViewGroup; |
Svetoslav Ganov | 5ac413a | 2010-10-05 15:59:25 -0700 | [diff] [blame] | 34 | import android.view.accessibility.AccessibilityEvent; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | |
Aurimas Liutikas | 99441c5 | 2016-10-11 16:48:32 -0700 | [diff] [blame] | 36 | import com.android.internal.R; |
| 37 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | /** |
Evan Millar | 3730bb1 | 2009-08-21 13:58:41 -0700 | [diff] [blame] | 39 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | * Displays a list of tab labels representing each page in the parent's tab |
Alan Viverette | 7eb09d1 | 2015-08-14 11:36:01 -0400 | [diff] [blame] | 41 | * collection. |
| 42 | * <p> |
| 43 | * The container object for this widget is {@link android.widget.TabHost TabHost}. |
| 44 | * When the user selects a tab, this object sends a message to the parent |
| 45 | * container, TabHost, to tell it to switch the displayed page. You typically |
| 46 | * won't use many methods directly on this object. The container TabHost is |
| 47 | * used to add labels, add the callback handler, and manage callbacks. You |
| 48 | * might call this object to iterate the list of tabs, or to tweak the layout |
| 49 | * of the tab list, but most methods should be called on the containing TabHost |
| 50 | * object. |
| 51 | * |
Romain Guy | 7883c97 | 2010-03-01 16:39:17 -0800 | [diff] [blame] | 52 | * @attr ref android.R.styleable#TabWidget_divider |
Romain Guy | 6b1e696 | 2010-03-29 14:38:41 -0700 | [diff] [blame] | 53 | * @attr ref android.R.styleable#TabWidget_tabStripEnabled |
| 54 | * @attr ref android.R.styleable#TabWidget_tabStripLeft |
| 55 | * @attr ref android.R.styleable#TabWidget_tabStripRight |
Charles Chen | 28a0e47 | 2019-07-24 19:45:16 +0800 | [diff] [blame] | 56 | * |
| 57 | * @deprecated new applications should use fragment APIs instead of this class: |
| 58 | * Use <a href="{@docRoot}guide/navigation/navigation-swipe-view">TabLayout and ViewPager</a> |
| 59 | * instead. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | */ |
Charles Chen | 28a0e47 | 2019-07-24 19:45:16 +0800 | [diff] [blame] | 61 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 62 | public class TabWidget extends LinearLayout implements OnFocusChangeListener { |
Alan Viverette | 7eb09d1 | 2015-08-14 11:36:01 -0400 | [diff] [blame] | 63 | private final Rect mBounds = new Rect(); |
| 64 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 | private OnTabSelectionChanged mSelectionChangedListener; |
Romain Guy | 61c9d4b | 2010-03-01 14:12:10 -0800 | [diff] [blame] | 66 | |
Gilles Debunne | 97dfd34 | 2010-10-20 16:31:15 -0700 | [diff] [blame] | 67 | // This value will be set to 0 as soon as the first tab is added to TabHost. |
Charles Chen | 5c0943d | 2019-07-30 17:21:47 +0800 | [diff] [blame] | 68 | @UnsupportedAppUsage(trackingBug = 137825207, maxTargetSdk = Build.VERSION_CODES.Q, |
| 69 | publicAlternatives = "Use {@code androidx.viewpager.widget.ViewPager} and " |
| 70 | + "{@code com.google.android.material.tabs.TabLayout} instead.\n" |
| 71 | + "See <a href=\"{@docRoot}guide/navigation/navigation-swipe-view" |
| 72 | + "\">TabLayout and ViewPager</a>") |
Gilles Debunne | 97dfd34 | 2010-10-20 16:31:15 -0700 | [diff] [blame] | 73 | private int mSelectedTab = -1; |
Romain Guy | 61c9d4b | 2010-03-01 14:12:10 -0800 | [diff] [blame] | 74 | |
Alan Viverette | 00e9401 | 2017-09-15 16:04:48 -0400 | [diff] [blame] | 75 | @Nullable |
Romain Guy | 65fe2c08 | 2010-03-29 12:27:30 -0700 | [diff] [blame] | 76 | private Drawable mLeftStrip; |
Alan Viverette | 00e9401 | 2017-09-15 16:04:48 -0400 | [diff] [blame] | 77 | |
| 78 | @Nullable |
Romain Guy | 65fe2c08 | 2010-03-29 12:27:30 -0700 | [diff] [blame] | 79 | private Drawable mRightStrip; |
Romain Guy | 61c9d4b | 2010-03-01 14:12:10 -0800 | [diff] [blame] | 80 | |
Charles Chen | 5c0943d | 2019-07-30 17:21:47 +0800 | [diff] [blame] | 81 | @UnsupportedAppUsage(trackingBug = 137825207, maxTargetSdk = Build.VERSION_CODES.Q, |
| 82 | publicAlternatives = "Use {@code androidx.viewpager.widget.ViewPager} and " |
| 83 | + "{@code com.google.android.material.tabs.TabLayout} instead.\n" |
| 84 | + "See <a href=\"{@docRoot}guide/navigation/navigation-swipe-view" |
| 85 | + "\">TabLayout and ViewPager</a>") |
Jack Veenstra | 5317514 | 2009-06-01 21:27:01 -0700 | [diff] [blame] | 86 | private boolean mDrawBottomStrips = true; |
Romain Guy | 61c9d4b | 2010-03-01 14:12:10 -0800 | [diff] [blame] | 87 | private boolean mStripMoved; |
| 88 | |
Alan Viverette | 7eb09d1 | 2015-08-14 11:36:01 -0400 | [diff] [blame] | 89 | // When positive, the widths and heights of tabs will be imposed so that |
| 90 | // they fit in parent. |
Gilles Debunne | c4d3f75 | 2011-01-25 20:11:45 -0800 | [diff] [blame] | 91 | private int mImposedTabsHeight = -1; |
| 92 | private int[] mImposedTabWidths; |
| 93 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 94 | public TabWidget(Context context) { |
| 95 | this(context, null); |
| 96 | } |
| 97 | |
| 98 | public TabWidget(Context context, AttributeSet attrs) { |
| 99 | this(context, attrs, com.android.internal.R.attr.tabWidgetStyle); |
| 100 | } |
| 101 | |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 102 | public TabWidget(Context context, AttributeSet attrs, int defStyleAttr) { |
| 103 | this(context, attrs, defStyleAttr, 0); |
| 104 | } |
| 105 | |
| 106 | public TabWidget(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { |
| 107 | super(context, attrs, defStyleAttr, defStyleRes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 108 | |
Jeff Sharkey | 11f4a48 | 2011-08-08 21:05:40 -0700 | [diff] [blame] | 109 | final TypedArray a = context.obtainStyledAttributes( |
Alan Viverette | 7eb09d1 | 2015-08-14 11:36:01 -0400 | [diff] [blame] | 110 | attrs, R.styleable.TabWidget, defStyleAttr, defStyleRes); |
Aurimas Liutikas | ab324cf | 2019-02-07 16:46:38 -0800 | [diff] [blame] | 111 | saveAttributeDataForStyleable(context, R.styleable.TabWidget, |
| 112 | attrs, a, defStyleAttr, defStyleRes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 113 | |
Alan Viverette | 7eb09d1 | 2015-08-14 11:36:01 -0400 | [diff] [blame] | 114 | mDrawBottomStrips = a.getBoolean(R.styleable.TabWidget_tabStripEnabled, mDrawBottomStrips); |
| 115 | |
| 116 | // Tests the target SDK version, as set in the Manifest. Could not be |
| 117 | // set using styles.xml in a values-v? directory which targets the |
| 118 | // current platform SDK version instead. |
| 119 | final boolean isTargetSdkDonutOrLower = |
| 120 | context.getApplicationInfo().targetSdkVersion <= Build.VERSION_CODES.DONUT; |
| 121 | |
| 122 | final boolean hasExplicitLeft = a.hasValueOrEmpty(R.styleable.TabWidget_tabStripLeft); |
| 123 | if (hasExplicitLeft) { |
| 124 | mLeftStrip = a.getDrawable(R.styleable.TabWidget_tabStripLeft); |
| 125 | } else if (isTargetSdkDonutOrLower) { |
| 126 | mLeftStrip = context.getDrawable(R.drawable.tab_bottom_left_v4); |
| 127 | } else { |
| 128 | mLeftStrip = context.getDrawable(R.drawable.tab_bottom_left); |
| 129 | } |
| 130 | |
| 131 | final boolean hasExplicitRight = a.hasValueOrEmpty(R.styleable.TabWidget_tabStripRight); |
| 132 | if (hasExplicitRight) { |
| 133 | mRightStrip = a.getDrawable(R.styleable.TabWidget_tabStripRight); |
| 134 | } else if (isTargetSdkDonutOrLower) { |
| 135 | mRightStrip = context.getDrawable(R.drawable.tab_bottom_right_v4); |
| 136 | } else { |
| 137 | mRightStrip = context.getDrawable(R.drawable.tab_bottom_right); |
| 138 | } |
Romain Guy | 61c9d4b | 2010-03-01 14:12:10 -0800 | [diff] [blame] | 139 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 140 | a.recycle(); |
Romain Guy | 61c9d4b | 2010-03-01 14:12:10 -0800 | [diff] [blame] | 141 | |
Alan Viverette | 7eb09d1 | 2015-08-14 11:36:01 -0400 | [diff] [blame] | 142 | setChildrenDrawingOrderEnabled(true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 143 | } |
Evan Millar | 3730bb1 | 2009-08-21 13:58:41 -0700 | [diff] [blame] | 144 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 145 | @Override |
| 146 | protected void onSizeChanged(int w, int h, int oldw, int oldh) { |
| 147 | mStripMoved = true; |
Alan Viverette | 7eb09d1 | 2015-08-14 11:36:01 -0400 | [diff] [blame] | 148 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 149 | super.onSizeChanged(w, h, oldw, oldh); |
| 150 | } |
| 151 | |
Evan Millar | 3730bb1 | 2009-08-21 13:58:41 -0700 | [diff] [blame] | 152 | @Override |
| 153 | protected int getChildDrawingOrder(int childCount, int i) { |
Gilles Debunne | 97dfd34 | 2010-10-20 16:31:15 -0700 | [diff] [blame] | 154 | if (mSelectedTab == -1) { |
Evan Millar | 3730bb1 | 2009-08-21 13:58:41 -0700 | [diff] [blame] | 155 | return i; |
Gilles Debunne | 97dfd34 | 2010-10-20 16:31:15 -0700 | [diff] [blame] | 156 | } else { |
| 157 | // Always draw the selected tab last, so that drop shadows are drawn |
| 158 | // in the correct z-order. |
| 159 | if (i == childCount - 1) { |
| 160 | return mSelectedTab; |
| 161 | } else if (i >= mSelectedTab) { |
| 162 | return i + 1; |
| 163 | } else { |
| 164 | return i; |
| 165 | } |
Evan Millar | 3730bb1 | 2009-08-21 13:58:41 -0700 | [diff] [blame] | 166 | } |
| 167 | } |
| 168 | |
Gilles Debunne | 6741c94 | 2011-01-25 20:11:45 -0800 | [diff] [blame] | 169 | @Override |
Alan Viverette | 7eb09d1 | 2015-08-14 11:36:01 -0400 | [diff] [blame] | 170 | void measureChildBeforeLayout(View child, int childIndex, int widthMeasureSpec, int totalWidth, |
Gilles Debunne | c4d3f75 | 2011-01-25 20:11:45 -0800 | [diff] [blame] | 171 | int heightMeasureSpec, int totalHeight) { |
Jeff Sharkey | 11f4a48 | 2011-08-08 21:05:40 -0700 | [diff] [blame] | 172 | if (!isMeasureWithLargestChildEnabled() && mImposedTabsHeight >= 0) { |
Gilles Debunne | c4d3f75 | 2011-01-25 20:11:45 -0800 | [diff] [blame] | 173 | widthMeasureSpec = MeasureSpec.makeMeasureSpec( |
| 174 | totalWidth + mImposedTabWidths[childIndex], MeasureSpec.EXACTLY); |
| 175 | heightMeasureSpec = MeasureSpec.makeMeasureSpec(mImposedTabsHeight, |
| 176 | MeasureSpec.EXACTLY); |
Gilles Debunne | 6741c94 | 2011-01-25 20:11:45 -0800 | [diff] [blame] | 177 | } |
| 178 | |
Gilles Debunne | c4d3f75 | 2011-01-25 20:11:45 -0800 | [diff] [blame] | 179 | super.measureChildBeforeLayout(child, childIndex, |
| 180 | widthMeasureSpec, totalWidth, heightMeasureSpec, totalHeight); |
| 181 | } |
| 182 | |
| 183 | @Override |
| 184 | void measureHorizontal(int widthMeasureSpec, int heightMeasureSpec) { |
Gilles Debunne | 52a5e658 | 2011-02-28 17:58:35 -0800 | [diff] [blame] | 185 | if (MeasureSpec.getMode(widthMeasureSpec) == MeasureSpec.UNSPECIFIED) { |
| 186 | super.measureHorizontal(widthMeasureSpec, heightMeasureSpec); |
| 187 | return; |
| 188 | } |
| 189 | |
Gilles Debunne | c4d3f75 | 2011-01-25 20:11:45 -0800 | [diff] [blame] | 190 | // First, measure with no constraint |
Filip Gruszczynski | b6824bf | 2015-04-13 09:16:25 -0700 | [diff] [blame] | 191 | final int width = MeasureSpec.getSize(widthMeasureSpec); |
Adam Powell | d5dbf4b | 2015-06-11 13:19:24 -0700 | [diff] [blame] | 192 | final int unspecifiedWidth = MeasureSpec.makeSafeMeasureSpec(width, |
| 193 | MeasureSpec.UNSPECIFIED); |
Gilles Debunne | c4d3f75 | 2011-01-25 20:11:45 -0800 | [diff] [blame] | 194 | mImposedTabsHeight = -1; |
Gilles Debunne | cd59feb | 2011-02-25 14:34:20 -0800 | [diff] [blame] | 195 | super.measureHorizontal(unspecifiedWidth, heightMeasureSpec); |
Gilles Debunne | c4d3f75 | 2011-01-25 20:11:45 -0800 | [diff] [blame] | 196 | |
Filip Gruszczynski | b6824bf | 2015-04-13 09:16:25 -0700 | [diff] [blame] | 197 | int extraWidth = getMeasuredWidth() - width; |
Gilles Debunne | c4d3f75 | 2011-01-25 20:11:45 -0800 | [diff] [blame] | 198 | if (extraWidth > 0) { |
| 199 | final int count = getChildCount(); |
| 200 | |
| 201 | int childCount = 0; |
Gilles Debunne | 6741c94 | 2011-01-25 20:11:45 -0800 | [diff] [blame] | 202 | for (int i = 0; i < count; i++) { |
| 203 | final View child = getChildAt(i); |
Gilles Debunne | c4d3f75 | 2011-01-25 20:11:45 -0800 | [diff] [blame] | 204 | if (child.getVisibility() == GONE) continue; |
| 205 | childCount++; |
Gilles Debunne | 6741c94 | 2011-01-25 20:11:45 -0800 | [diff] [blame] | 206 | } |
Gilles Debunne | c4d3f75 | 2011-01-25 20:11:45 -0800 | [diff] [blame] | 207 | |
| 208 | if (childCount > 0) { |
| 209 | if (mImposedTabWidths == null || mImposedTabWidths.length != count) { |
| 210 | mImposedTabWidths = new int[count]; |
| 211 | } |
| 212 | for (int i = 0; i < count; i++) { |
| 213 | final View child = getChildAt(i); |
| 214 | if (child.getVisibility() == GONE) continue; |
| 215 | final int childWidth = child.getMeasuredWidth(); |
| 216 | final int delta = extraWidth / childCount; |
| 217 | final int newWidth = Math.max(0, childWidth - delta); |
| 218 | mImposedTabWidths[i] = newWidth; |
| 219 | // Make sure the extra width is evenly distributed, no int division remainder |
| 220 | extraWidth -= childWidth - newWidth; // delta may have been clamped |
| 221 | childCount--; |
| 222 | mImposedTabsHeight = Math.max(mImposedTabsHeight, child.getMeasuredHeight()); |
| 223 | } |
| 224 | } |
| 225 | } |
| 226 | |
Alan Viverette | 7eb09d1 | 2015-08-14 11:36:01 -0400 | [diff] [blame] | 227 | // Measure again, this time with imposed tab widths and respecting |
| 228 | // initial spec request. |
Gilles Debunne | 52a5e658 | 2011-02-28 17:58:35 -0800 | [diff] [blame] | 229 | super.measureHorizontal(widthMeasureSpec, heightMeasureSpec); |
Gilles Debunne | 6741c94 | 2011-01-25 20:11:45 -0800 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | /** |
Jack Veenstra | 5317514 | 2009-06-01 21:27:01 -0700 | [diff] [blame] | 233 | * Returns the tab indicator view at the given index. |
| 234 | * |
| 235 | * @param index the zero-based index of the tab indicator view to return |
| 236 | * @return the tab indicator view at the given index |
| 237 | */ |
| 238 | public View getChildTabViewAt(int index) { |
Jack Veenstra | 5317514 | 2009-06-01 21:27:01 -0700 | [diff] [blame] | 239 | return getChildAt(index); |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * Returns the number of tab indicator views. |
Alan Viverette | 7eb09d1 | 2015-08-14 11:36:01 -0400 | [diff] [blame] | 244 | * |
| 245 | * @return the number of tab indicator views |
Jack Veenstra | 5317514 | 2009-06-01 21:27:01 -0700 | [diff] [blame] | 246 | */ |
| 247 | public int getTabCount() { |
Jeff Sharkey | 11f4a48 | 2011-08-08 21:05:40 -0700 | [diff] [blame] | 248 | return getChildCount(); |
Jack Veenstra | 5317514 | 2009-06-01 21:27:01 -0700 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | /** |
| 252 | * Sets the drawable to use as a divider between the tab indicators. |
Alan Viverette | 7eb09d1 | 2015-08-14 11:36:01 -0400 | [diff] [blame] | 253 | * |
Jack Veenstra | 5317514 | 2009-06-01 21:27:01 -0700 | [diff] [blame] | 254 | * @param drawable the divider drawable |
Alan Viverette | 7eb09d1 | 2015-08-14 11:36:01 -0400 | [diff] [blame] | 255 | * @attr ref android.R.styleable#TabWidget_divider |
Jack Veenstra | 5317514 | 2009-06-01 21:27:01 -0700 | [diff] [blame] | 256 | */ |
Gilles Debunne | 6741c94 | 2011-01-25 20:11:45 -0800 | [diff] [blame] | 257 | @Override |
Alan Viverette | 7eb09d1 | 2015-08-14 11:36:01 -0400 | [diff] [blame] | 258 | public void setDividerDrawable(@Nullable Drawable drawable) { |
Jeff Sharkey | 11f4a48 | 2011-08-08 21:05:40 -0700 | [diff] [blame] | 259 | super.setDividerDrawable(drawable); |
Jack Veenstra | 5317514 | 2009-06-01 21:27:01 -0700 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | /** |
| 263 | * Sets the drawable to use as a divider between the tab indicators. |
Alan Viverette | 7eb09d1 | 2015-08-14 11:36:01 -0400 | [diff] [blame] | 264 | * |
| 265 | * @param resId the resource identifier of the drawable to use as a divider |
| 266 | * @attr ref android.R.styleable#TabWidget_divider |
Jack Veenstra | 5317514 | 2009-06-01 21:27:01 -0700 | [diff] [blame] | 267 | */ |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 268 | public void setDividerDrawable(@DrawableRes int resId) { |
Alan Viverette | 8eea3ea | 2014-02-03 18:40:20 -0800 | [diff] [blame] | 269 | setDividerDrawable(mContext.getDrawable(resId)); |
Jack Veenstra | 5317514 | 2009-06-01 21:27:01 -0700 | [diff] [blame] | 270 | } |
Alan Viverette | 7eb09d1 | 2015-08-14 11:36:01 -0400 | [diff] [blame] | 271 | |
Romain Guy | 61c9d4b | 2010-03-01 14:12:10 -0800 | [diff] [blame] | 272 | /** |
Alan Viverette | 7eb09d1 | 2015-08-14 11:36:01 -0400 | [diff] [blame] | 273 | * Sets the drawable to use as the left part of the strip below the tab |
| 274 | * indicators. |
| 275 | * |
Romain Guy | 61c9d4b | 2010-03-01 14:12:10 -0800 | [diff] [blame] | 276 | * @param drawable the left strip drawable |
Alan Viverette | d78036b | 2015-08-14 13:53:56 -0400 | [diff] [blame] | 277 | * @see #getLeftStripDrawable() |
Alan Viverette | 7eb09d1 | 2015-08-14 11:36:01 -0400 | [diff] [blame] | 278 | * @attr ref android.R.styleable#TabWidget_tabStripLeft |
Romain Guy | 61c9d4b | 2010-03-01 14:12:10 -0800 | [diff] [blame] | 279 | */ |
Alan Viverette | 7eb09d1 | 2015-08-14 11:36:01 -0400 | [diff] [blame] | 280 | public void setLeftStripDrawable(@Nullable Drawable drawable) { |
Romain Guy | 65fe2c08 | 2010-03-29 12:27:30 -0700 | [diff] [blame] | 281 | mLeftStrip = drawable; |
Romain Guy | 42c7988 | 2010-03-01 17:20:57 -0800 | [diff] [blame] | 282 | requestLayout(); |
| 283 | invalidate(); |
Romain Guy | 61c9d4b | 2010-03-01 14:12:10 -0800 | [diff] [blame] | 284 | } |
Jack Veenstra | 5317514 | 2009-06-01 21:27:01 -0700 | [diff] [blame] | 285 | |
| 286 | /** |
Alan Viverette | 7eb09d1 | 2015-08-14 11:36:01 -0400 | [diff] [blame] | 287 | * Sets the drawable to use as the left part of the strip below the tab |
| 288 | * indicators. |
| 289 | * |
| 290 | * @param resId the resource identifier of the drawable to use as the left |
| 291 | * strip drawable |
Alan Viverette | d78036b | 2015-08-14 13:53:56 -0400 | [diff] [blame] | 292 | * @see #getLeftStripDrawable() |
Alan Viverette | 7eb09d1 | 2015-08-14 11:36:01 -0400 | [diff] [blame] | 293 | * @attr ref android.R.styleable#TabWidget_tabStripLeft |
Romain Guy | 61c9d4b | 2010-03-01 14:12:10 -0800 | [diff] [blame] | 294 | */ |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 295 | public void setLeftStripDrawable(@DrawableRes int resId) { |
Alan Viverette | 8eea3ea | 2014-02-03 18:40:20 -0800 | [diff] [blame] | 296 | setLeftStripDrawable(mContext.getDrawable(resId)); |
Romain Guy | 61c9d4b | 2010-03-01 14:12:10 -0800 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | /** |
Alan Viverette | d78036b | 2015-08-14 13:53:56 -0400 | [diff] [blame] | 300 | * @return the drawable used as the left part of the strip below the tab |
| 301 | * indicators, may be {@code null} |
| 302 | * @see #setLeftStripDrawable(int) |
| 303 | * @see #setLeftStripDrawable(Drawable) |
| 304 | * @attr ref android.R.styleable#TabWidget_tabStripLeft |
| 305 | */ |
| 306 | @Nullable |
| 307 | public Drawable getLeftStripDrawable() { |
| 308 | return mLeftStrip; |
| 309 | } |
| 310 | |
| 311 | /** |
Alan Viverette | 7eb09d1 | 2015-08-14 11:36:01 -0400 | [diff] [blame] | 312 | * Sets the drawable to use as the right part of the strip below the tab |
| 313 | * indicators. |
| 314 | * |
Marco Nelissen | 189f65c | 2010-03-22 10:59:00 -0700 | [diff] [blame] | 315 | * @param drawable the right strip drawable |
Alan Viverette | d78036b | 2015-08-14 13:53:56 -0400 | [diff] [blame] | 316 | * @see #getRightStripDrawable() |
Alan Viverette | 7eb09d1 | 2015-08-14 11:36:01 -0400 | [diff] [blame] | 317 | * @attr ref android.R.styleable#TabWidget_tabStripRight |
Romain Guy | 61c9d4b | 2010-03-01 14:12:10 -0800 | [diff] [blame] | 318 | */ |
Alan Viverette | 7eb09d1 | 2015-08-14 11:36:01 -0400 | [diff] [blame] | 319 | public void setRightStripDrawable(@Nullable Drawable drawable) { |
Romain Guy | 65fe2c08 | 2010-03-29 12:27:30 -0700 | [diff] [blame] | 320 | mRightStrip = drawable; |
Romain Guy | 42c7988 | 2010-03-01 17:20:57 -0800 | [diff] [blame] | 321 | requestLayout(); |
Jeff Sharkey | 11f4a48 | 2011-08-08 21:05:40 -0700 | [diff] [blame] | 322 | invalidate(); |
| 323 | } |
Romain Guy | 61c9d4b | 2010-03-01 14:12:10 -0800 | [diff] [blame] | 324 | |
| 325 | /** |
Alan Viverette | 7eb09d1 | 2015-08-14 11:36:01 -0400 | [diff] [blame] | 326 | * Sets the drawable to use as the right part of the strip below the tab |
| 327 | * indicators. |
| 328 | * |
| 329 | * @param resId the resource identifier of the drawable to use as the right |
| 330 | * strip drawable |
Alan Viverette | d78036b | 2015-08-14 13:53:56 -0400 | [diff] [blame] | 331 | * @see #getRightStripDrawable() |
Alan Viverette | 7eb09d1 | 2015-08-14 11:36:01 -0400 | [diff] [blame] | 332 | * @attr ref android.R.styleable#TabWidget_tabStripRight |
Romain Guy | 61c9d4b | 2010-03-01 14:12:10 -0800 | [diff] [blame] | 333 | */ |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 334 | public void setRightStripDrawable(@DrawableRes int resId) { |
Alan Viverette | 8eea3ea | 2014-02-03 18:40:20 -0800 | [diff] [blame] | 335 | setRightStripDrawable(mContext.getDrawable(resId)); |
Romain Guy | 61c9d4b | 2010-03-01 14:12:10 -0800 | [diff] [blame] | 336 | } |
Jeff Sharkey | 11f4a48 | 2011-08-08 21:05:40 -0700 | [diff] [blame] | 337 | |
Romain Guy | 61c9d4b | 2010-03-01 14:12:10 -0800 | [diff] [blame] | 338 | /** |
Alan Viverette | d78036b | 2015-08-14 13:53:56 -0400 | [diff] [blame] | 339 | * @return the drawable used as the right part of the strip below the tab |
| 340 | * indicators, may be {@code null} |
| 341 | * @see #setRightStripDrawable(int) |
| 342 | * @see #setRightStripDrawable(Drawable) |
| 343 | * @attr ref android.R.styleable#TabWidget_tabStripRight |
| 344 | */ |
| 345 | @Nullable |
| 346 | public Drawable getRightStripDrawable() { |
| 347 | return mRightStrip; |
| 348 | } |
| 349 | |
| 350 | /** |
Jack Veenstra | 5317514 | 2009-06-01 21:27:01 -0700 | [diff] [blame] | 351 | * Controls whether the bottom strips on the tab indicators are drawn or |
| 352 | * not. The default is to draw them. If the user specifies a custom |
| 353 | * view for the tab indicators, then the TabHost class calls this method |
| 354 | * to disable drawing of the bottom strips. |
Romain Guy | 61c9d4b | 2010-03-01 14:12:10 -0800 | [diff] [blame] | 355 | * @param stripEnabled true if the bottom strips should be drawn. |
Jack Veenstra | 5317514 | 2009-06-01 21:27:01 -0700 | [diff] [blame] | 356 | */ |
Romain Guy | 61c9d4b | 2010-03-01 14:12:10 -0800 | [diff] [blame] | 357 | public void setStripEnabled(boolean stripEnabled) { |
| 358 | mDrawBottomStrips = stripEnabled; |
Romain Guy | 42c7988 | 2010-03-01 17:20:57 -0800 | [diff] [blame] | 359 | invalidate(); |
Romain Guy | 61c9d4b | 2010-03-01 14:12:10 -0800 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | /** |
| 363 | * Indicates whether the bottom strips on the tab indicators are drawn |
| 364 | * or not. |
| 365 | */ |
| 366 | public boolean isStripEnabled() { |
| 367 | return mDrawBottomStrips; |
Jack Veenstra | 5317514 | 2009-06-01 21:27:01 -0700 | [diff] [blame] | 368 | } |
| 369 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 370 | @Override |
| 371 | public void childDrawableStateChanged(View child) { |
Bjorn Bringert | acdef59 | 2009-12-10 15:58:49 +0000 | [diff] [blame] | 372 | if (getTabCount() > 0 && child == getChildTabViewAt(mSelectedTab)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 373 | // To make sure that the bottom strip is redrawn |
| 374 | invalidate(); |
| 375 | } |
| 376 | super.childDrawableStateChanged(child); |
| 377 | } |
Evan Millar | 3730bb1 | 2009-08-21 13:58:41 -0700 | [diff] [blame] | 378 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 379 | @Override |
| 380 | public void dispatchDraw(Canvas canvas) { |
| 381 | super.dispatchDraw(canvas); |
| 382 | |
Bjorn Bringert | acdef59 | 2009-12-10 15:58:49 +0000 | [diff] [blame] | 383 | // Do nothing if there are no tabs. |
| 384 | if (getTabCount() == 0) return; |
| 385 | |
Jack Veenstra | 5317514 | 2009-06-01 21:27:01 -0700 | [diff] [blame] | 386 | // If the user specified a custom view for the tab indicators, then |
| 387 | // do not draw the bottom strips. |
| 388 | if (!mDrawBottomStrips) { |
| 389 | // Skip drawing the bottom strips. |
| 390 | return; |
| 391 | } |
| 392 | |
Romain Guy | 61c9d4b | 2010-03-01 14:12:10 -0800 | [diff] [blame] | 393 | final View selectedChild = getChildTabViewAt(mSelectedTab); |
Evan Millar | 3730bb1 | 2009-08-21 13:58:41 -0700 | [diff] [blame] | 394 | |
Romain Guy | 65fe2c08 | 2010-03-29 12:27:30 -0700 | [diff] [blame] | 395 | final Drawable leftStrip = mLeftStrip; |
| 396 | final Drawable rightStrip = mRightStrip; |
Romain Guy | 61c9d4b | 2010-03-01 14:12:10 -0800 | [diff] [blame] | 397 | |
Alan Viverette | 00e9401 | 2017-09-15 16:04:48 -0400 | [diff] [blame] | 398 | if (leftStrip != null) { |
| 399 | leftStrip.setState(selectedChild.getDrawableState()); |
| 400 | } |
| 401 | if (rightStrip != null) { |
| 402 | rightStrip.setState(selectedChild.getDrawableState()); |
| 403 | } |
Evan Millar | 3730bb1 | 2009-08-21 13:58:41 -0700 | [diff] [blame] | 404 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 405 | if (mStripMoved) { |
Romain Guy | 61c9d4b | 2010-03-01 14:12:10 -0800 | [diff] [blame] | 406 | final Rect bounds = mBounds; |
| 407 | bounds.left = selectedChild.getLeft(); |
| 408 | bounds.right = selectedChild.getRight(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 409 | final int myHeight = getHeight(); |
Alan Viverette | 00e9401 | 2017-09-15 16:04:48 -0400 | [diff] [blame] | 410 | if (leftStrip != null) { |
| 411 | leftStrip.setBounds(Math.min(0, bounds.left - leftStrip.getIntrinsicWidth()), |
| 412 | myHeight - leftStrip.getIntrinsicHeight(), bounds.left, myHeight); |
| 413 | } |
| 414 | if (rightStrip != null) { |
| 415 | rightStrip.setBounds(bounds.right, myHeight - rightStrip.getIntrinsicHeight(), |
| 416 | Math.max(getWidth(), bounds.right + rightStrip.getIntrinsicWidth()), |
| 417 | myHeight); |
| 418 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 419 | mStripMoved = false; |
| 420 | } |
Evan Millar | 3730bb1 | 2009-08-21 13:58:41 -0700 | [diff] [blame] | 421 | |
Alan Viverette | 00e9401 | 2017-09-15 16:04:48 -0400 | [diff] [blame] | 422 | if (leftStrip != null) { |
| 423 | leftStrip.draw(canvas); |
| 424 | } |
| 425 | if (rightStrip != null) { |
| 426 | rightStrip.draw(canvas); |
| 427 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | /** |
| 431 | * Sets the current tab. |
Alan Viverette | 7eb09d1 | 2015-08-14 11:36:01 -0400 | [diff] [blame] | 432 | * <p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 433 | * This method is used to bring a tab to the front of the Widget, |
| 434 | * and is used to post to the rest of the UI that a different tab |
| 435 | * has been brought to the foreground. |
Alan Viverette | 7eb09d1 | 2015-08-14 11:36:01 -0400 | [diff] [blame] | 436 | * <p> |
Evan Millar | 3730bb1 | 2009-08-21 13:58:41 -0700 | [diff] [blame] | 437 | * Note, this is separate from the traditional "focus" that is |
| 438 | * employed from the view logic. |
Alan Viverette | 7eb09d1 | 2015-08-14 11:36:01 -0400 | [diff] [blame] | 439 | * <p> |
Evan Millar | 3730bb1 | 2009-08-21 13:58:41 -0700 | [diff] [blame] | 440 | * For instance, if we have a list in a tabbed view, a user may be |
| 441 | * navigating up and down the list, moving the UI focus (orange |
| 442 | * highlighting) through the list items. The cursor movement does |
| 443 | * not effect the "selected" tab though, because what is being |
| 444 | * scrolled through is all on the same tab. The selected tab only |
| 445 | * changes when we navigate between tabs (moving from the list view |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 446 | * to the next tabbed view, in this example). |
Alan Viverette | 7eb09d1 | 2015-08-14 11:36:01 -0400 | [diff] [blame] | 447 | * <p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 448 | * To move both the focus AND the selected tab at once, please use |
Kevin Hufnagle | e71346f | 2019-07-18 12:50:31 -0700 | [diff] [blame] | 449 | * {@link #focusCurrentTab}. Normally, the view logic takes care of |
Evan Millar | 3730bb1 | 2009-08-21 13:58:41 -0700 | [diff] [blame] | 450 | * adjusting the focus, so unless you're circumventing the UI, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 451 | * you'll probably just focus your interest here. |
Evan Millar | 3730bb1 | 2009-08-21 13:58:41 -0700 | [diff] [blame] | 452 | * |
Alan Viverette | 7eb09d1 | 2015-08-14 11:36:01 -0400 | [diff] [blame] | 453 | * @param index the index of the tab that you want to indicate as the |
| 454 | * selected tab (tab brought to the front of the widget) |
| 455 | * @see #focusCurrentTab |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 456 | */ |
| 457 | public void setCurrentTab(int index) { |
Svetoslav Ganov | 5ac413a | 2010-10-05 15:59:25 -0700 | [diff] [blame] | 458 | if (index < 0 || index >= getTabCount() || index == mSelectedTab) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 459 | return; |
| 460 | } |
| 461 | |
Gilles Debunne | 97dfd34 | 2010-10-20 16:31:15 -0700 | [diff] [blame] | 462 | if (mSelectedTab != -1) { |
| 463 | getChildTabViewAt(mSelectedTab).setSelected(false); |
| 464 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 465 | mSelectedTab = index; |
Jack Veenstra | 5317514 | 2009-06-01 21:27:01 -0700 | [diff] [blame] | 466 | getChildTabViewAt(mSelectedTab).setSelected(true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 467 | mStripMoved = true; |
Svetoslav Ganov | 736c275 | 2011-04-22 18:30:36 -0700 | [diff] [blame] | 468 | } |
| 469 | |
Dianne Hackborn | a7bb6fb | 2015-02-03 18:13:40 -0800 | [diff] [blame] | 470 | @Override |
| 471 | public CharSequence getAccessibilityClassName() { |
| 472 | return TabWidget.class.getName(); |
| 473 | } |
| 474 | |
Alan Viverette | a54956a | 2015-01-07 16:05:02 -0800 | [diff] [blame] | 475 | /** @hide */ |
Svetoslav Ganov | 736c275 | 2011-04-22 18:30:36 -0700 | [diff] [blame] | 476 | @Override |
Alan Viverette | a54956a | 2015-01-07 16:05:02 -0800 | [diff] [blame] | 477 | public void onInitializeAccessibilityEventInternal(AccessibilityEvent event) { |
| 478 | super.onInitializeAccessibilityEventInternal(event); |
Svetoslav Ganov | 5ac413a | 2010-10-05 15:59:25 -0700 | [diff] [blame] | 479 | event.setItemCount(getTabCount()); |
| 480 | event.setCurrentItemIndex(mSelectedTab); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 481 | } |
Evan Millar | 3730bb1 | 2009-08-21 13:58:41 -0700 | [diff] [blame] | 482 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 483 | /** |
| 484 | * Sets the current tab and focuses the UI on it. |
Evan Millar | 3730bb1 | 2009-08-21 13:58:41 -0700 | [diff] [blame] | 485 | * This method makes sure that the focused tab matches the selected |
| 486 | * tab, normally at {@link #setCurrentTab}. Normally this would not |
| 487 | * be an issue if we go through the UI, since the UI is responsible |
| 488 | * for calling TabWidget.onFocusChanged(), but in the case where we |
| 489 | * are selecting the tab programmatically, we'll need to make sure |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 490 | * focus keeps up. |
Evan Millar | 3730bb1 | 2009-08-21 13:58:41 -0700 | [diff] [blame] | 491 | * |
| 492 | * @param index The tab that you want focused (highlighted in orange) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 493 | * and selected (tab brought to the front of the widget) |
Evan Millar | 3730bb1 | 2009-08-21 13:58:41 -0700 | [diff] [blame] | 494 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 495 | * @see #setCurrentTab |
| 496 | */ |
| 497 | public void focusCurrentTab(int index) { |
| 498 | final int oldTab = mSelectedTab; |
| 499 | |
| 500 | // set the tab |
| 501 | setCurrentTab(index); |
Evan Millar | 3730bb1 | 2009-08-21 13:58:41 -0700 | [diff] [blame] | 502 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 503 | // change the focus if applicable. |
| 504 | if (oldTab != index) { |
Jack Veenstra | 5317514 | 2009-06-01 21:27:01 -0700 | [diff] [blame] | 505 | getChildTabViewAt(index).requestFocus(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 506 | } |
| 507 | } |
Evan Millar | 3730bb1 | 2009-08-21 13:58:41 -0700 | [diff] [blame] | 508 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 509 | @Override |
| 510 | public void setEnabled(boolean enabled) { |
| 511 | super.setEnabled(enabled); |
Evan Millar | 3730bb1 | 2009-08-21 13:58:41 -0700 | [diff] [blame] | 512 | |
Jeff Sharkey | 11f4a48 | 2011-08-08 21:05:40 -0700 | [diff] [blame] | 513 | final int count = getTabCount(); |
Jack Veenstra | 5317514 | 2009-06-01 21:27:01 -0700 | [diff] [blame] | 514 | for (int i = 0; i < count; i++) { |
Alan Viverette | 7eb09d1 | 2015-08-14 11:36:01 -0400 | [diff] [blame] | 515 | final View child = getChildTabViewAt(i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 516 | child.setEnabled(enabled); |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | @Override |
| 521 | public void addView(View child) { |
| 522 | if (child.getLayoutParams() == null) { |
| 523 | final LinearLayout.LayoutParams lp = new LayoutParams( |
Alan Viverette | 7eb09d1 | 2015-08-14 11:36:01 -0400 | [diff] [blame] | 524 | 0, ViewGroup.LayoutParams.MATCH_PARENT, 1.0f); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 525 | lp.setMargins(0, 0, 0, 0); |
| 526 | child.setLayoutParams(lp); |
| 527 | } |
| 528 | |
| 529 | // Ensure you can navigate to the tab with the keyboard, and you can touch it |
| 530 | child.setFocusable(true); |
| 531 | child.setClickable(true); |
| 532 | |
Vladislav Kaznacheev | 2a848ff | 2016-09-23 10:16:16 -0700 | [diff] [blame] | 533 | if (child.getPointerIcon() == null) { |
| 534 | child.setPointerIcon(PointerIcon.getSystemIcon(getContext(), PointerIcon.TYPE_HAND)); |
| 535 | } |
| 536 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 537 | super.addView(child); |
| 538 | |
| 539 | // TODO: detect this via geometry with a tabwidget listener rather |
| 540 | // than potentially interfere with the view's listener |
Jack Veenstra | 5317514 | 2009-06-01 21:27:01 -0700 | [diff] [blame] | 541 | child.setOnClickListener(new TabClickListener(getTabCount() - 1)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 542 | } |
| 543 | |
Svetoslav Ganov | 5ac413a | 2010-10-05 15:59:25 -0700 | [diff] [blame] | 544 | @Override |
Jeff Sharkey | cd2ca40 | 2011-06-10 15:14:07 -0700 | [diff] [blame] | 545 | public void removeAllViews() { |
| 546 | super.removeAllViews(); |
| 547 | mSelectedTab = -1; |
| 548 | } |
| 549 | |
Vladislav Kaznacheev | 2a848ff | 2016-09-23 10:16:16 -0700 | [diff] [blame] | 550 | @Override |
| 551 | public PointerIcon onResolvePointerIcon(MotionEvent event, int pointerIndex) { |
| 552 | if (!isEnabled()) { |
| 553 | return null; |
| 554 | } |
| 555 | return super.onResolvePointerIcon(event, pointerIndex); |
| 556 | } |
| 557 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 558 | /** |
Alan Viverette | 7eb09d1 | 2015-08-14 11:36:01 -0400 | [diff] [blame] | 559 | * Provides a way for {@link TabHost} to be notified that the user clicked |
| 560 | * on a tab indicator. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 561 | */ |
Charles Chen | 5c0943d | 2019-07-30 17:21:47 +0800 | [diff] [blame] | 562 | @UnsupportedAppUsage(trackingBug = 137825207, maxTargetSdk = Build.VERSION_CODES.Q, |
| 563 | publicAlternatives = "Use {@code androidx.viewpager.widget.ViewPager} and " |
| 564 | + "{@code com.google.android.material.tabs.TabLayout} instead.\n" |
| 565 | + "See <a href=\"{@docRoot}guide/navigation/navigation-swipe-view" |
| 566 | + "\">TabLayout and ViewPager</a>") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 567 | void setTabSelectionListener(OnTabSelectionChanged listener) { |
| 568 | mSelectionChangedListener = listener; |
| 569 | } |
| 570 | |
Alan Viverette | 1352a36 | 2015-08-17 14:22:22 -0400 | [diff] [blame] | 571 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 572 | public void onFocusChange(View v, boolean hasFocus) { |
Alan Viverette | 1352a36 | 2015-08-17 14:22:22 -0400 | [diff] [blame] | 573 | // No-op. Tab selection is separate from keyboard focus. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | // registered with each tab indicator so we can notify tab host |
| 577 | private class TabClickListener implements OnClickListener { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 578 | private final int mTabIndex; |
| 579 | |
| 580 | private TabClickListener(int tabIndex) { |
| 581 | mTabIndex = tabIndex; |
| 582 | } |
| 583 | |
| 584 | public void onClick(View v) { |
| 585 | mSelectionChangedListener.onTabSelectionChanged(mTabIndex, true); |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | /** |
Alan Viverette | 7eb09d1 | 2015-08-14 11:36:01 -0400 | [diff] [blame] | 590 | * Lets {@link TabHost} know that the user clicked on a tab indicator. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 591 | */ |
Alan Viverette | 7eb09d1 | 2015-08-14 11:36:01 -0400 | [diff] [blame] | 592 | interface OnTabSelectionChanged { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 593 | /** |
| 594 | * Informs the TabHost which tab was selected. It also indicates |
| 595 | * if the tab was clicked/pressed or just focused into. |
Evan Millar | 3730bb1 | 2009-08-21 13:58:41 -0700 | [diff] [blame] | 596 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 597 | * @param tabIndex index of the tab that was selected |
Alan Viverette | 7eb09d1 | 2015-08-14 11:36:01 -0400 | [diff] [blame] | 598 | * @param clicked whether the selection changed due to a touch/click or |
| 599 | * due to focus entering the tab through navigation. |
| 600 | * {@code true} if it was due to a press/click and |
| 601 | * {@code false} otherwise. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 602 | */ |
| 603 | void onTabSelectionChanged(int tabIndex, boolean clicked); |
| 604 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 605 | } |