| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.widget; |
| 18 |
|
| 19 |
import android.annotation.DrawableRes; |
| 20 |
import android.annotation.Nullable; |
| 21 |
import android.compat.annotation.UnsupportedAppUsage; |
| 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; |
| 27 |
import android.os.Build; |
| 28 |
import android.util.AttributeSet; |
| 29 |
import android.view.MotionEvent; |
| 30 |
import android.view.PointerIcon; |
| 31 |
import android.view.View; |
| 32 |
import android.view.View.OnFocusChangeListener; |
| 33 |
import android.view.ViewGroup; |
| 34 |
import android.view.accessibility.AccessibilityEvent; |
| 35 |
|
| 36 |
import com.android.internal.R; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
@Deprecated |
| 62 |
public class TabWidget extends LinearLayout implements OnFocusChangeListener { |
| 63 |
private final Rect mBounds = new Rect(); |
| 64 |
|
| 65 |
private OnTabSelectionChanged mSelectionChangedListener; |
| 66 |
|
| 67 |
|
| 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>") |
| 73 |
private int mSelectedTab = -1; |
| 74 |
|
| 75 |
@Nullable |
| 76 |
private Drawable mLeftStrip; |
| 77 |
|
| 78 |
@Nullable |
| 79 |
private Drawable mRightStrip; |
| 80 |
|
| 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>") |
| 86 |
private boolean mDrawBottomStrips = true; |
| 87 |
private boolean mStripMoved; |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
private int mImposedTabsHeight = -1; |
| 92 |
private int[] mImposedTabWidths; |
| 93 |
|
| 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 |
|
| 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); |
| 108 |
|
| 109 |
final TypedArray a = context.obtainStyledAttributes( |
| 110 |
attrs, R.styleable.TabWidget, defStyleAttr, defStyleRes); |
| 111 |
saveAttributeDataForStyleable(context, R.styleable.TabWidget, |
| 112 |
attrs, a, defStyleAttr, defStyleRes); |
| 113 |
|
| 114 |
mDrawBottomStrips = a.getBoolean(R.styleable.TabWidget_tabStripEnabled, mDrawBottomStrips); |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 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 |
} |
| 139 |
|
| 140 |
a.recycle(); |
| 141 |
|
| 142 |
setChildrenDrawingOrderEnabled(true); |
| 143 |
} |
| 144 |
|
| 145 |
@Override |
| 146 |
protected void onSizeChanged(int w, int h, int oldw, int oldh) { |
| 147 |
mStripMoved = true; |
| 148 |
|
| 149 |
super.onSizeChanged(w, h, oldw, oldh); |
| 150 |
} |
| 151 |
|
| 152 |
@Override |
| 153 |
protected int getChildDrawingOrder(int childCount, int i) { |
| 154 |
if (mSelectedTab == -1) { |
| 155 |
return i; |
| 156 |
} else { |
| 157 |
|
| 158 |
|
| 159 |
if (i == childCount - 1) { |
| 160 |
return mSelectedTab; |
| 161 |
} else if (i >= mSelectedTab) { |
| 162 |
return i + 1; |
| 163 |
} else { |
| 164 |
return i; |
| 165 |
} |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
@Override |
| 170 |
void measureChildBeforeLayout(View child, int childIndex, int widthMeasureSpec, int totalWidth, |
| 171 |
int heightMeasureSpec, int totalHeight) { |
| 172 |
if (!isMeasureWithLargestChildEnabled() && mImposedTabsHeight >= 0) { |
| 173 |
widthMeasureSpec = MeasureSpec.makeMeasureSpec( |
| 174 |
totalWidth + mImposedTabWidths[childIndex], MeasureSpec.EXACTLY); |
| 175 |
heightMeasureSpec = MeasureSpec.makeMeasureSpec(mImposedTabsHeight, |
| 176 |
MeasureSpec.EXACTLY); |
| 177 |
} |
| 178 |
|
| 179 |
super.measureChildBeforeLayout(child, childIndex, |
| 180 |
widthMeasureSpec, totalWidth, heightMeasureSpec, totalHeight); |
| 181 |
} |
| 182 |
|
| 183 |
@Override |
| 184 |
void measureHorizontal(int widthMeasureSpec, int heightMeasureSpec) { |
| 185 |
if (MeasureSpec.getMode(widthMeasureSpec) == MeasureSpec.UNSPECIFIED) { |
| 186 |
super.measureHorizontal(widthMeasureSpec, heightMeasureSpec); |
| 187 |
return; |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
final int width = MeasureSpec.getSize(widthMeasureSpec); |
| 192 |
final int unspecifiedWidth = MeasureSpec.makeSafeMeasureSpec(width, |
| 193 |
MeasureSpec.UNSPECIFIED); |
| 194 |
mImposedTabsHeight = -1; |
| 195 |
super.measureHorizontal(unspecifiedWidth, heightMeasureSpec); |
| 196 |
|
| 197 |
int extraWidth = getMeasuredWidth() - width; |
| 198 |
if (extraWidth > 0) { |
| 199 |
final int count = getChildCount(); |
| 200 |
|
| 201 |
int childCount = 0; |
| 202 |
for (int i = 0; i < count; i++) { |
| 203 |
final View child = getChildAt(i); |
| 204 |
if (child.getVisibility() == GONE) continue; |
| 205 |
childCount++; |
| 206 |
} |
| 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 |
|
| 220 |
extraWidth -= childWidth - newWidth; |
| 221 |
childCount--; |
| 222 |
mImposedTabsHeight = Math.max(mImposedTabsHeight, child.getMeasuredHeight()); |
| 223 |
} |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
super.measureHorizontal(widthMeasureSpec, heightMeasureSpec); |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
public View getChildTabViewAt(int index) { |
| 239 |
return getChildAt(index); |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
public int getTabCount() { |
| 248 |
return getChildCount(); |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
@Override |
| 258 |
public void setDividerDrawable(@Nullable Drawable drawable) { |
| 259 |
super.setDividerDrawable(drawable); |
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
public void setDividerDrawable(@DrawableRes int resId) { |
| 269 |
setDividerDrawable(mContext.getDrawable(resId)); |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
public void setLeftStripDrawable(@Nullable Drawable drawable) { |
| 281 |
mLeftStrip = drawable; |
| 282 |
requestLayout(); |
| 283 |
invalidate(); |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
public void setLeftStripDrawable(@DrawableRes int resId) { |
| 296 |
setLeftStripDrawable(mContext.getDrawable(resId)); |
| 297 |
} |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
@Nullable |
| 307 |
public Drawable getLeftStripDrawable() { |
| 308 |
return mLeftStrip; |
| 309 |
} |
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
public void setRightStripDrawable(@Nullable Drawable drawable) { |
| 320 |
mRightStrip = drawable; |
| 321 |
requestLayout(); |
| 322 |
invalidate(); |
| 323 |
} |
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
public void setRightStripDrawable(@DrawableRes int resId) { |
| 335 |
setRightStripDrawable(mContext.getDrawable(resId)); |
| 336 |
} |
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
@Nullable |
| 346 |
public Drawable getRightStripDrawable() { |
| 347 |
return mRightStrip; |
| 348 |
} |
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
public void setStripEnabled(boolean stripEnabled) { |
| 358 |
mDrawBottomStrips = stripEnabled; |
| 359 |
invalidate(); |
| 360 |
} |
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
public boolean isStripEnabled() { |
| 367 |
return mDrawBottomStrips; |
| 368 |
} |
| 369 |
|
| 370 |
@Override |
| 371 |
public void childDrawableStateChanged(View child) { |
| 372 |
if (getTabCount() > 0 && child == getChildTabViewAt(mSelectedTab)) { |
| 373 |
|
| 374 |
invalidate(); |
| 375 |
} |
| 376 |
super.childDrawableStateChanged(child); |
| 377 |
} |
| 378 |
|
| 379 |
@Override |
| 380 |
public void dispatchDraw(Canvas canvas) { |
| 381 |
super.dispatchDraw(canvas); |
| 382 |
|
| 383 |
|
| 384 |
if (getTabCount() == 0) return; |
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
if (!mDrawBottomStrips) { |
| 389 |
|
| 390 |
return; |
| 391 |
} |
| 392 |
|
| 393 |
final View selectedChild = getChildTabViewAt(mSelectedTab); |
| 394 |
|
| 395 |
final Drawable leftStrip = mLeftStrip; |
| 396 |
final Drawable rightStrip = mRightStrip; |
| 397 |
|
| 398 |
if (leftStrip != null) { |
| 399 |
leftStrip.setState(selectedChild.getDrawableState()); |
| 400 |
} |
| 401 |
if (rightStrip != null) { |
| 402 |
rightStrip.setState(selectedChild.getDrawableState()); |
| 403 |
} |
| 404 |
|
| 405 |
if (mStripMoved) { |
| 406 |
final Rect bounds = mBounds; |
| 407 |
bounds.left = selectedChild.getLeft(); |
| 408 |
bounds.right = selectedChild.getRight(); |
| 409 |
final int myHeight = getHeight(); |
| 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 |
} |
| 419 |
mStripMoved = false; |
| 420 |
} |
| 421 |
|
| 422 |
if (leftStrip != null) { |
| 423 |
leftStrip.draw(canvas); |
| 424 |
} |
| 425 |
if (rightStrip != null) { |
| 426 |
rightStrip.draw(canvas); |
| 427 |
} |
| 428 |
} |
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
public void setCurrentTab(int index) { |
| 458 |
if (index < 0 || index >= getTabCount() || index == mSelectedTab) { |
| 459 |
return; |
| 460 |
} |
| 461 |
|
| 462 |
if (mSelectedTab != -1) { |
| 463 |
getChildTabViewAt(mSelectedTab).setSelected(false); |
| 464 |
} |
| 465 |
mSelectedTab = index; |
| 466 |
getChildTabViewAt(mSelectedTab).setSelected(true); |
| 467 |
mStripMoved = true; |
| 468 |
} |
| 469 |
|
| 470 |
@Override |
| 471 |
public CharSequence getAccessibilityClassName() { |
| 472 |
return TabWidget.class.getName(); |
| 473 |
} |
| 474 |
|
| 475 |
|
| 476 |
@Override |
| 477 |
public void onInitializeAccessibilityEventInternal(AccessibilityEvent event) { |
| 478 |
super.onInitializeAccessibilityEventInternal(event); |
| 479 |
event.setItemCount(getTabCount()); |
| 480 |
event.setCurrentItemIndex(mSelectedTab); |
| 481 |
} |
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
public void focusCurrentTab(int index) { |
| 498 |
final int oldTab = mSelectedTab; |
| 499 |
|
| 500 |
|
| 501 |
setCurrentTab(index); |
| 502 |
|
| 503 |
|
| 504 |
if (oldTab != index) { |
| 505 |
getChildTabViewAt(index).requestFocus(); |
| 506 |
} |
| 507 |
} |
| 508 |
|
| 509 |
@Override |
| 510 |
public void setEnabled(boolean enabled) { |
| 511 |
super.setEnabled(enabled); |
| 512 |
|
| 513 |
final int count = getTabCount(); |
| 514 |
for (int i = 0; i < count; i++) { |
| 515 |
final View child = getChildTabViewAt(i); |
| 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( |
| 524 |
0, ViewGroup.LayoutParams.MATCH_PARENT, 1.0f); |
| 525 |
lp.setMargins(0, 0, 0, 0); |
| 526 |
child.setLayoutParams(lp); |
| 527 |
} |
| 528 |
|
| 529 |
|
| 530 |
child.setFocusable(true); |
| 531 |
child.setClickable(true); |
| 532 |
|
| 533 |
if (child.getPointerIcon() == null) { |
| 534 |
child.setPointerIcon(PointerIcon.getSystemIcon(getContext(), PointerIcon.TYPE_HAND)); |
| 535 |
} |
| 536 |
|
| 537 |
super.addView(child); |
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
child.setOnClickListener(new TabClickListener(getTabCount() - 1)); |
| 542 |
} |
| 543 |
|
| 544 |
@Override |
| 545 |
public void removeAllViews() { |
| 546 |
super.removeAllViews(); |
| 547 |
mSelectedTab = -1; |
| 548 |
} |
| 549 |
|
| 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 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 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>") |
| 567 |
void setTabSelectionListener(OnTabSelectionChanged listener) { |
| 568 |
mSelectionChangedListener = listener; |
| 569 |
} |
| 570 |
|
| 571 |
@Override |
| 572 |
public void onFocusChange(View v, boolean hasFocus) { |
| 573 |
|
| 574 |
} |
| 575 |
|
| 576 |
|
| 577 |
private class TabClickListener implements OnClickListener { |
| 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 |
|
| 590 |
|
| 591 |
|
| 592 |
interface OnTabSelectionChanged { |
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
void onTabSelectionChanged(int tabIndex, boolean clicked); |
| 604 |
} |
| 605 |
} |