| 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.NonNull; |
| 20 |
import android.annotation.Nullable; |
| 21 |
import android.app.LocalActivityManager; |
| 22 |
import android.compat.annotation.UnsupportedAppUsage; |
| 23 |
import android.content.Context; |
| 24 |
import android.content.Intent; |
| 25 |
import android.content.res.TypedArray; |
| 26 |
import android.graphics.drawable.Drawable; |
| 27 |
import android.os.Build; |
| 28 |
import android.text.TextUtils; |
| 29 |
import android.util.AttributeSet; |
| 30 |
import android.view.KeyEvent; |
| 31 |
import android.view.LayoutInflater; |
| 32 |
import android.view.SoundEffectConstants; |
| 33 |
import android.view.View; |
| 34 |
import android.view.ViewGroup; |
| 35 |
import android.view.ViewTreeObserver; |
| 36 |
import android.view.Window; |
| 37 |
|
| 38 |
import com.android.internal.R; |
| 39 |
|
| 40 |
import java.util.ArrayList; |
| 41 |
import java.util.List; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
@Deprecated |
| 54 |
public class TabHost extends FrameLayout implements ViewTreeObserver.OnTouchModeChangeListener { |
| 55 |
|
| 56 |
private static final int TABWIDGET_LOCATION_LEFT = 0; |
| 57 |
private static final int TABWIDGET_LOCATION_TOP = 1; |
| 58 |
private static final int TABWIDGET_LOCATION_RIGHT = 2; |
| 59 |
private static final int TABWIDGET_LOCATION_BOTTOM = 3; |
| 60 |
private TabWidget mTabWidget; |
| 61 |
private FrameLayout mTabContent; |
| 62 |
@UnsupportedAppUsage(trackingBug = 137825207, maxTargetSdk = Build.VERSION_CODES.Q, |
| 63 |
publicAlternatives = "Use {@code androidx.viewpager.widget.ViewPager} and " |
| 64 |
+ "{@code com.google.android.material.tabs.TabLayout} instead.\n" |
| 65 |
+ "See <a href=\"{@docRoot}guide/navigation/navigation-swipe-view" |
| 66 |
+ "\">TabLayout and ViewPager</a>") |
| 67 |
private List<TabSpec> mTabSpecs = new ArrayList<TabSpec>(2); |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
@UnsupportedAppUsage(trackingBug = 137825207, maxTargetSdk = Build.VERSION_CODES.Q, |
| 73 |
publicAlternatives = "Use {@code androidx.viewpager.widget.ViewPager} and " |
| 74 |
+ "{@code com.google.android.material.tabs.TabLayout} instead.\n" |
| 75 |
+ "See <a href=\"{@docRoot}guide/navigation/navigation-swipe-view" |
| 76 |
+ "\">TabLayout and ViewPager</a>") |
| 77 |
protected int mCurrentTab = -1; |
| 78 |
private View mCurrentView = null; |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
protected LocalActivityManager mLocalActivityManager = null; |
| 84 |
@UnsupportedAppUsage(trackingBug = 137825207, maxTargetSdk = Build.VERSION_CODES.Q, |
| 85 |
publicAlternatives = "Use {@code androidx.viewpager.widget.ViewPager} and " |
| 86 |
+ "{@code com.google.android.material.tabs.TabLayout} instead.\n" |
| 87 |
+ "See <a href=\"{@docRoot}guide/navigation/navigation-swipe-view" |
| 88 |
+ "\">TabLayout and ViewPager</a>") |
| 89 |
private OnTabChangeListener mOnTabChangeListener; |
| 90 |
private OnKeyListener mTabKeyListener; |
| 91 |
|
| 92 |
private int mTabLayoutId; |
| 93 |
|
| 94 |
public TabHost(Context context) { |
| 95 |
super(context); |
| 96 |
initTabHost(); |
| 97 |
} |
| 98 |
|
| 99 |
public TabHost(Context context, AttributeSet attrs) { |
| 100 |
this(context, attrs, com.android.internal.R.attr.tabWidgetStyle); |
| 101 |
} |
| 102 |
|
| 103 |
public TabHost(Context context, AttributeSet attrs, int defStyleAttr) { |
| 104 |
this(context, attrs, defStyleAttr, 0); |
| 105 |
} |
| 106 |
|
| 107 |
public TabHost(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { |
| 108 |
super(context, attrs); |
| 109 |
|
| 110 |
final TypedArray a = context.obtainStyledAttributes( |
| 111 |
attrs, com.android.internal.R.styleable.TabWidget, defStyleAttr, defStyleRes); |
| 112 |
saveAttributeDataForStyleable(context, com.android.internal.R.styleable.TabWidget, |
| 113 |
attrs, a, defStyleAttr, defStyleRes); |
| 114 |
|
| 115 |
mTabLayoutId = a.getResourceId(R.styleable.TabWidget_tabLayout, 0); |
| 116 |
a.recycle(); |
| 117 |
|
| 118 |
if (mTabLayoutId == 0) { |
| 119 |
|
| 120 |
|
| 121 |
mTabLayoutId = R.layout.tab_indicator_holo; |
| 122 |
} |
| 123 |
|
| 124 |
initTabHost(); |
| 125 |
} |
| 126 |
|
| 127 |
private void initTabHost() { |
| 128 |
setFocusableInTouchMode(true); |
| 129 |
setDescendantFocusability(FOCUS_AFTER_DESCENDANTS); |
| 130 |
|
| 131 |
mCurrentTab = -1; |
| 132 |
mCurrentView = null; |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
@NonNull |
| 142 |
public TabSpec newTabSpec(@NonNull String tag) { |
| 143 |
if (tag == null) { |
| 144 |
throw new IllegalArgumentException("tag must be non-null"); |
| 145 |
} |
| 146 |
return new TabSpec(tag); |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
public void setup() { |
| 161 |
mTabWidget = findViewById(com.android.internal.R.id.tabs); |
| 162 |
if (mTabWidget == null) { |
| 163 |
throw new RuntimeException( |
| 164 |
"Your TabHost must have a TabWidget whose id attribute is 'android.R.id.tabs'"); |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
mTabKeyListener = new OnKeyListener() { |
| 170 |
public boolean onKey(View v, int keyCode, KeyEvent event) { |
| 171 |
if (KeyEvent.isModifierKey(keyCode)) { |
| 172 |
return false; |
| 173 |
} |
| 174 |
switch (keyCode) { |
| 175 |
case KeyEvent.KEYCODE_DPAD_CENTER: |
| 176 |
case KeyEvent.KEYCODE_DPAD_LEFT: |
| 177 |
case KeyEvent.KEYCODE_DPAD_RIGHT: |
| 178 |
case KeyEvent.KEYCODE_DPAD_UP: |
| 179 |
case KeyEvent.KEYCODE_DPAD_DOWN: |
| 180 |
case KeyEvent.KEYCODE_TAB: |
| 181 |
case KeyEvent.KEYCODE_SPACE: |
| 182 |
case KeyEvent.KEYCODE_ENTER: |
| 183 |
return false; |
| 184 |
|
| 185 |
} |
| 186 |
mTabContent.requestFocus(View.FOCUS_FORWARD); |
| 187 |
return mTabContent.dispatchKeyEvent(event); |
| 188 |
} |
| 189 |
|
| 190 |
}; |
| 191 |
|
| 192 |
mTabWidget.setTabSelectionListener(new TabWidget.OnTabSelectionChanged() { |
| 193 |
public void onTabSelectionChanged(int tabIndex, boolean clicked) { |
| 194 |
setCurrentTab(tabIndex); |
| 195 |
if (clicked) { |
| 196 |
mTabContent.requestFocus(View.FOCUS_FORWARD); |
| 197 |
} |
| 198 |
} |
| 199 |
}); |
| 200 |
|
| 201 |
mTabContent = findViewById(com.android.internal.R.id.tabcontent); |
| 202 |
if (mTabContent == null) { |
| 203 |
throw new RuntimeException( |
| 204 |
"Your TabHost must have a FrameLayout whose id attribute is " |
| 205 |
+ "'android.R.id.tabcontent'"); |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
@Override |
| 211 |
public void sendAccessibilityEventInternal(int eventType) { |
| 212 |
|
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
public void setup(LocalActivityManager activityGroup) { |
| 223 |
setup(); |
| 224 |
mLocalActivityManager = activityGroup; |
| 225 |
} |
| 226 |
|
| 227 |
@Override |
| 228 |
public void onTouchModeChanged(boolean isInTouchMode) { |
| 229 |
|
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
public void addTab(TabSpec tabSpec) { |
| 239 |
|
| 240 |
if (tabSpec.mIndicatorStrategy == null) { |
| 241 |
throw new IllegalArgumentException("you must specify a way to create the tab indicator."); |
| 242 |
} |
| 243 |
|
| 244 |
if (tabSpec.mContentStrategy == null) { |
| 245 |
throw new IllegalArgumentException("you must specify a way to create the tab content"); |
| 246 |
} |
| 247 |
View tabIndicator = tabSpec.mIndicatorStrategy.createIndicatorView(); |
| 248 |
tabIndicator.setOnKeyListener(mTabKeyListener); |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
if (tabSpec.mIndicatorStrategy instanceof ViewIndicatorStrategy) { |
| 253 |
mTabWidget.setStripEnabled(false); |
| 254 |
} |
| 255 |
|
| 256 |
mTabWidget.addView(tabIndicator); |
| 257 |
mTabSpecs.add(tabSpec); |
| 258 |
|
| 259 |
if (mCurrentTab == -1) { |
| 260 |
setCurrentTab(0); |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
public void clearAllTabs() { |
| 269 |
mTabWidget.removeAllViews(); |
| 270 |
initTabHost(); |
| 271 |
mTabContent.removeAllViews(); |
| 272 |
mTabSpecs.clear(); |
| 273 |
requestLayout(); |
| 274 |
invalidate(); |
| 275 |
} |
| 276 |
|
| 277 |
public TabWidget getTabWidget() { |
| 278 |
return mTabWidget; |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
@Nullable |
| 287 |
public int getCurrentTab() { |
| 288 |
return mCurrentTab; |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
@Nullable |
| 298 |
public String getCurrentTabTag() { |
| 299 |
if (mCurrentTab >= 0 && mCurrentTab < mTabSpecs.size()) { |
| 300 |
return mTabSpecs.get(mCurrentTab).getTag(); |
| 301 |
} |
| 302 |
return null; |
| 303 |
} |
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
@Nullable |
| 312 |
public View getCurrentTabView() { |
| 313 |
if (mCurrentTab >= 0 && mCurrentTab < mTabSpecs.size()) { |
| 314 |
return mTabWidget.getChildTabViewAt(mCurrentTab); |
| 315 |
} |
| 316 |
return null; |
| 317 |
} |
| 318 |
|
| 319 |
public View getCurrentView() { |
| 320 |
return mCurrentView; |
| 321 |
} |
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
public void setCurrentTabByTag(String tag) { |
| 329 |
for (int i = 0, count = mTabSpecs.size(); i < count; i++) { |
| 330 |
if (mTabSpecs.get(i).getTag().equals(tag)) { |
| 331 |
setCurrentTab(i); |
| 332 |
break; |
| 333 |
} |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
public FrameLayout getTabContentView() { |
| 341 |
return mTabContent; |
| 342 |
} |
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
private int getTabWidgetLocation() { |
| 350 |
int location = TABWIDGET_LOCATION_TOP; |
| 351 |
|
| 352 |
switch (mTabWidget.getOrientation()) { |
| 353 |
case LinearLayout.VERTICAL: |
| 354 |
location = (mTabContent.getLeft() < mTabWidget.getLeft()) ? TABWIDGET_LOCATION_RIGHT |
| 355 |
: TABWIDGET_LOCATION_LEFT; |
| 356 |
break; |
| 357 |
case LinearLayout.HORIZONTAL: |
| 358 |
default: |
| 359 |
location = (mTabContent.getTop() < mTabWidget.getTop()) ? TABWIDGET_LOCATION_BOTTOM |
| 360 |
: TABWIDGET_LOCATION_TOP; |
| 361 |
break; |
| 362 |
} |
| 363 |
return location; |
| 364 |
} |
| 365 |
|
| 366 |
@Override |
| 367 |
public boolean dispatchKeyEvent(KeyEvent event) { |
| 368 |
final boolean handled = super.dispatchKeyEvent(event); |
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
if (!handled |
| 374 |
&& (event.getAction() == KeyEvent.ACTION_DOWN) |
| 375 |
&& (mCurrentView != null) |
| 376 |
&& (mCurrentView.isRootNamespace()) |
| 377 |
&& (mCurrentView.hasFocus())) { |
| 378 |
int keyCodeShouldChangeFocus = KeyEvent.KEYCODE_DPAD_UP; |
| 379 |
int directionShouldChangeFocus = View.FOCUS_UP; |
| 380 |
int soundEffect = SoundEffectConstants.NAVIGATION_UP; |
| 381 |
|
| 382 |
switch (getTabWidgetLocation()) { |
| 383 |
case TABWIDGET_LOCATION_LEFT: |
| 384 |
keyCodeShouldChangeFocus = KeyEvent.KEYCODE_DPAD_LEFT; |
| 385 |
directionShouldChangeFocus = View.FOCUS_LEFT; |
| 386 |
soundEffect = SoundEffectConstants.NAVIGATION_LEFT; |
| 387 |
break; |
| 388 |
case TABWIDGET_LOCATION_RIGHT: |
| 389 |
keyCodeShouldChangeFocus = KeyEvent.KEYCODE_DPAD_RIGHT; |
| 390 |
directionShouldChangeFocus = View.FOCUS_RIGHT; |
| 391 |
soundEffect = SoundEffectConstants.NAVIGATION_RIGHT; |
| 392 |
break; |
| 393 |
case TABWIDGET_LOCATION_BOTTOM: |
| 394 |
keyCodeShouldChangeFocus = KeyEvent.KEYCODE_DPAD_DOWN; |
| 395 |
directionShouldChangeFocus = View.FOCUS_DOWN; |
| 396 |
soundEffect = SoundEffectConstants.NAVIGATION_DOWN; |
| 397 |
break; |
| 398 |
case TABWIDGET_LOCATION_TOP: |
| 399 |
default: |
| 400 |
keyCodeShouldChangeFocus = KeyEvent.KEYCODE_DPAD_UP; |
| 401 |
directionShouldChangeFocus = View.FOCUS_UP; |
| 402 |
soundEffect = SoundEffectConstants.NAVIGATION_UP; |
| 403 |
break; |
| 404 |
} |
| 405 |
if (event.getKeyCode() == keyCodeShouldChangeFocus |
| 406 |
&& mCurrentView.findFocus().focusSearch(directionShouldChangeFocus) == null) { |
| 407 |
mTabWidget.getChildTabViewAt(mCurrentTab).requestFocus(); |
| 408 |
playSoundEffect(soundEffect); |
| 409 |
return true; |
| 410 |
} |
| 411 |
} |
| 412 |
return handled; |
| 413 |
} |
| 414 |
|
| 415 |
|
| 416 |
@Override |
| 417 |
public void dispatchWindowFocusChanged(boolean hasFocus) { |
| 418 |
if (mCurrentView != null){ |
| 419 |
mCurrentView.dispatchWindowFocusChanged(hasFocus); |
| 420 |
} |
| 421 |
} |
| 422 |
|
| 423 |
@Override |
| 424 |
public CharSequence getAccessibilityClassName() { |
| 425 |
return TabHost.class.getName(); |
| 426 |
} |
| 427 |
|
| 428 |
public void setCurrentTab(int index) { |
| 429 |
if (index < 0 || index >= mTabSpecs.size()) { |
| 430 |
return; |
| 431 |
} |
| 432 |
|
| 433 |
if (index == mCurrentTab) { |
| 434 |
return; |
| 435 |
} |
| 436 |
|
| 437 |
|
| 438 |
if (mCurrentTab != -1) { |
| 439 |
mTabSpecs.get(mCurrentTab).mContentStrategy.tabClosed(); |
| 440 |
} |
| 441 |
|
| 442 |
mCurrentTab = index; |
| 443 |
final TabHost.TabSpec spec = mTabSpecs.get(index); |
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
mTabWidget.focusCurrentTab(mCurrentTab); |
| 448 |
|
| 449 |
|
| 450 |
mCurrentView = spec.mContentStrategy.getContentView(); |
| 451 |
|
| 452 |
if (mCurrentView.getParent() == null) { |
| 453 |
mTabContent |
| 454 |
.addView( |
| 455 |
mCurrentView, |
| 456 |
new ViewGroup.LayoutParams( |
| 457 |
ViewGroup.LayoutParams.MATCH_PARENT, |
| 458 |
ViewGroup.LayoutParams.MATCH_PARENT)); |
| 459 |
} |
| 460 |
|
| 461 |
if (!mTabWidget.hasFocus()) { |
| 462 |
|
| 463 |
|
| 464 |
mCurrentView.requestFocus(); |
| 465 |
} |
| 466 |
|
| 467 |
|
| 468 |
invokeOnTabChangeListener(); |
| 469 |
} |
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
public void setOnTabChangedListener(OnTabChangeListener l) { |
| 478 |
mOnTabChangeListener = l; |
| 479 |
} |
| 480 |
|
| 481 |
private void invokeOnTabChangeListener() { |
| 482 |
if (mOnTabChangeListener != null) { |
| 483 |
mOnTabChangeListener.onTabChanged(getCurrentTabTag()); |
| 484 |
} |
| 485 |
} |
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
public interface OnTabChangeListener { |
| 491 |
void onTabChanged(String tabId); |
| 492 |
} |
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
public interface TabContentFactory { |
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
View createTabContent(String tag); |
| 509 |
} |
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
public class TabSpec { |
| 526 |
|
| 527 |
private final @NonNull String mTag; |
| 528 |
|
| 529 |
@UnsupportedAppUsage(trackingBug = 137825207, maxTargetSdk = Build.VERSION_CODES.Q, |
| 530 |
publicAlternatives = "Use {@code androidx.viewpager.widget.ViewPager} and " |
| 531 |
+ "{@code com.google.android.material.tabs.TabLayout} instead.\n" |
| 532 |
+ "See <a href=\"{@docRoot}guide/navigation/navigation-swipe-view" |
| 533 |
+ "\">TabLayout and ViewPager</a>") |
| 534 |
private IndicatorStrategy mIndicatorStrategy; |
| 535 |
@UnsupportedAppUsage(trackingBug = 137825207, maxTargetSdk = Build.VERSION_CODES.Q, |
| 536 |
publicAlternatives = "Use {@code androidx.viewpager.widget.ViewPager} and " |
| 537 |
+ "{@code com.google.android.material.tabs.TabLayout} instead.\n" |
| 538 |
+ "See <a href=\"{@docRoot}guide/navigation/navigation-swipe-view" |
| 539 |
+ "\">TabLayout and ViewPager</a>") |
| 540 |
private ContentStrategy mContentStrategy; |
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
private TabSpec(@NonNull String tag) { |
| 548 |
mTag = tag; |
| 549 |
} |
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
public TabSpec setIndicator(CharSequence label) { |
| 555 |
mIndicatorStrategy = new LabelIndicatorStrategy(label); |
| 556 |
return this; |
| 557 |
} |
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
public TabSpec setIndicator(CharSequence label, Drawable icon) { |
| 563 |
mIndicatorStrategy = new LabelAndIconIndicatorStrategy(label, icon); |
| 564 |
return this; |
| 565 |
} |
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
public TabSpec setIndicator(View view) { |
| 571 |
mIndicatorStrategy = new ViewIndicatorStrategy(view); |
| 572 |
return this; |
| 573 |
} |
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
public TabSpec setContent(int viewId) { |
| 580 |
mContentStrategy = new ViewIdContentStrategy(viewId); |
| 581 |
return this; |
| 582 |
} |
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
public TabSpec setContent(TabContentFactory contentFactory) { |
| 589 |
mContentStrategy = new FactoryContentStrategy(mTag, contentFactory); |
| 590 |
return this; |
| 591 |
} |
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
public TabSpec setContent(Intent intent) { |
| 597 |
mContentStrategy = new IntentContentStrategy(mTag, intent); |
| 598 |
return this; |
| 599 |
} |
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
@NonNull |
| 607 |
public String getTag() { |
| 608 |
return mTag; |
| 609 |
} |
| 610 |
} |
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
private static interface IndicatorStrategy { |
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
View createIndicatorView(); |
| 621 |
} |
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
private static interface ContentStrategy { |
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
View getContentView(); |
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
void tabClosed(); |
| 638 |
} |
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
private class LabelIndicatorStrategy implements IndicatorStrategy { |
| 644 |
|
| 645 |
private final CharSequence mLabel; |
| 646 |
|
| 647 |
private LabelIndicatorStrategy(CharSequence label) { |
| 648 |
mLabel = label; |
| 649 |
} |
| 650 |
|
| 651 |
public View createIndicatorView() { |
| 652 |
final Context context = getContext(); |
| 653 |
LayoutInflater inflater = |
| 654 |
(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
| 655 |
View tabIndicator = inflater.inflate(mTabLayoutId, |
| 656 |
mTabWidget, |
| 657 |
false); |
| 658 |
|
| 659 |
final TextView tv = tabIndicator.findViewById(R.id.title); |
| 660 |
tv.setText(mLabel); |
| 661 |
|
| 662 |
if (context.getApplicationInfo().targetSdkVersion <= Build.VERSION_CODES.DONUT) { |
| 663 |
|
| 664 |
tabIndicator.setBackgroundResource(R.drawable.tab_indicator_v4); |
| 665 |
tv.setTextColor(context.getColorStateList(R.color.tab_indicator_text_v4)); |
| 666 |
} |
| 667 |
|
| 668 |
return tabIndicator; |
| 669 |
} |
| 670 |
} |
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
private class LabelAndIconIndicatorStrategy implements IndicatorStrategy { |
| 676 |
|
| 677 |
private final CharSequence mLabel; |
| 678 |
private final Drawable mIcon; |
| 679 |
|
| 680 |
private LabelAndIconIndicatorStrategy(CharSequence label, Drawable icon) { |
| 681 |
mLabel = label; |
| 682 |
mIcon = icon; |
| 683 |
} |
| 684 |
|
| 685 |
public View createIndicatorView() { |
| 686 |
final Context context = getContext(); |
| 687 |
LayoutInflater inflater = |
| 688 |
(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
| 689 |
View tabIndicator = inflater.inflate(mTabLayoutId, |
| 690 |
mTabWidget, |
| 691 |
false); |
| 692 |
|
| 693 |
final TextView tv = tabIndicator.findViewById(R.id.title); |
| 694 |
final ImageView iconView = tabIndicator.findViewById(R.id.icon); |
| 695 |
|
| 696 |
|
| 697 |
final boolean exclusive = iconView.getVisibility() == View.GONE; |
| 698 |
final boolean bindIcon = !exclusive || TextUtils.isEmpty(mLabel); |
| 699 |
|
| 700 |
tv.setText(mLabel); |
| 701 |
|
| 702 |
if (bindIcon && mIcon != null) { |
| 703 |
iconView.setImageDrawable(mIcon); |
| 704 |
iconView.setVisibility(VISIBLE); |
| 705 |
} |
| 706 |
|
| 707 |
if (context.getApplicationInfo().targetSdkVersion <= Build.VERSION_CODES.DONUT) { |
| 708 |
|
| 709 |
tabIndicator.setBackgroundResource(R.drawable.tab_indicator_v4); |
| 710 |
tv.setTextColor(context.getColorStateList(R.color.tab_indicator_text_v4)); |
| 711 |
} |
| 712 |
|
| 713 |
return tabIndicator; |
| 714 |
} |
| 715 |
} |
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
private class ViewIndicatorStrategy implements IndicatorStrategy { |
| 721 |
|
| 722 |
private final View mView; |
| 723 |
|
| 724 |
private ViewIndicatorStrategy(View view) { |
| 725 |
mView = view; |
| 726 |
} |
| 727 |
|
| 728 |
public View createIndicatorView() { |
| 729 |
return mView; |
| 730 |
} |
| 731 |
} |
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
private class ViewIdContentStrategy implements ContentStrategy { |
| 737 |
|
| 738 |
private final View mView; |
| 739 |
|
| 740 |
private ViewIdContentStrategy(int viewId) { |
| 741 |
mView = mTabContent.findViewById(viewId); |
| 742 |
if (mView != null) { |
| 743 |
mView.setVisibility(View.GONE); |
| 744 |
} else { |
| 745 |
throw new RuntimeException("Could not create tab content because " + |
| 746 |
"could not find view with id " + viewId); |
| 747 |
} |
| 748 |
} |
| 749 |
|
| 750 |
public View getContentView() { |
| 751 |
mView.setVisibility(View.VISIBLE); |
| 752 |
return mView; |
| 753 |
} |
| 754 |
|
| 755 |
public void tabClosed() { |
| 756 |
mView.setVisibility(View.GONE); |
| 757 |
} |
| 758 |
} |
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
private class FactoryContentStrategy implements ContentStrategy { |
| 764 |
private View mTabContent; |
| 765 |
private final CharSequence mTag; |
| 766 |
private TabContentFactory mFactory; |
| 767 |
|
| 768 |
public FactoryContentStrategy(CharSequence tag, TabContentFactory factory) { |
| 769 |
mTag = tag; |
| 770 |
mFactory = factory; |
| 771 |
} |
| 772 |
|
| 773 |
public View getContentView() { |
| 774 |
if (mTabContent == null) { |
| 775 |
mTabContent = mFactory.createTabContent(mTag.toString()); |
| 776 |
} |
| 777 |
mTabContent.setVisibility(View.VISIBLE); |
| 778 |
return mTabContent; |
| 779 |
} |
| 780 |
|
| 781 |
public void tabClosed() { |
| 782 |
mTabContent.setVisibility(View.GONE); |
| 783 |
} |
| 784 |
} |
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
private class IntentContentStrategy implements ContentStrategy { |
| 791 |
|
| 792 |
private final String mTag; |
| 793 |
private final Intent mIntent; |
| 794 |
|
| 795 |
private View mLaunchedView; |
| 796 |
|
| 797 |
private IntentContentStrategy(String tag, Intent intent) { |
| 798 |
mTag = tag; |
| 799 |
mIntent = intent; |
| 800 |
} |
| 801 |
|
| 802 |
@UnsupportedAppUsage(trackingBug = 137825207, maxTargetSdk = Build.VERSION_CODES.Q, |
| 803 |
publicAlternatives = "Use {@code androidx.viewpager.widget.ViewPager} and " |
| 804 |
+ "{@code com.google.android.material.tabs.TabLayout} instead.\n" |
| 805 |
+ "See <a href=\"{@docRoot}guide/navigation/navigation-swipe-view" |
| 806 |
+ "\">TabLayout and ViewPager</a>") |
| 807 |
public View getContentView() { |
| 808 |
if (mLocalActivityManager == null) { |
| 809 |
throw new IllegalStateException("Did you forget to call 'public void setup(LocalActivityManager activityGroup)'?"); |
| 810 |
} |
| 811 |
final Window w = mLocalActivityManager.startActivity( |
| 812 |
mTag, mIntent); |
| 813 |
final View wd = w != null ? w.getDecorView() : null; |
| 814 |
if (mLaunchedView != wd && mLaunchedView != null) { |
| 815 |
if (mLaunchedView.getParent() != null) { |
| 816 |
mTabContent.removeView(mLaunchedView); |
| 817 |
} |
| 818 |
} |
| 819 |
mLaunchedView = wd; |
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
|
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
if (mLaunchedView != null) { |
| 828 |
mLaunchedView.setVisibility(View.VISIBLE); |
| 829 |
mLaunchedView.setFocusableInTouchMode(true); |
| 830 |
((ViewGroup) mLaunchedView).setDescendantFocusability( |
| 831 |
FOCUS_AFTER_DESCENDANTS); |
| 832 |
} |
| 833 |
return mLaunchedView; |
| 834 |
} |
| 835 |
|
| 836 |
@UnsupportedAppUsage(trackingBug = 137825207, maxTargetSdk = Build.VERSION_CODES.Q, |
| 837 |
publicAlternatives = "Use {@code androidx.viewpager.widget.ViewPager} and " |
| 838 |
+ "{@code com.google.android.material.tabs.TabLayout} instead.\n" |
| 839 |
+ "See <a href=\"{@docRoot}guide/navigation/navigation-swipe-view" |
| 840 |
+ "\">TabLayout and ViewPager</a>") |
| 841 |
public void tabClosed() { |
| 842 |
if (mLaunchedView != null) { |
| 843 |
mLaunchedView.setVisibility(View.GONE); |
| 844 |
} |
| 845 |
} |
| 846 |
} |
| 847 |
|
| 848 |
} |