Skip to content

Commit

Permalink
[TabLayout] Setting a custom view on the tab should replace the curre…
Browse files Browse the repository at this point in the history
…nt custom view

PiperOrigin-RevId: 473057129
  • Loading branch information
imhappi authored and afohrman committed Sep 12, 2022
1 parent 6643695 commit 3c0c9e7
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/java/com/google/android/material/tabs/TabLayout.java
Expand Up @@ -2649,6 +2649,12 @@ final void update() {
if (customParent != null) {
((ViewGroup) customParent).removeView(custom);
}
if (customView != null) {
final ViewParent customViewParent = customView.getParent();
if (customViewParent != null) {
((ViewGroup) customViewParent).removeView(customView);
}
}
addView(custom);
}
customView = custom;
Expand Down
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (C) 2022 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<View xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/my_custom_alternate_tab"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Expand Up @@ -498,6 +498,20 @@ private void testSetScrollPosition(final boolean isLtr) throws Throwable {
Espresso.unregisterIdlingResources(idler);
}

@Test
@UiThreadTest
public void testSetCustomTabReplacesCustomView() {
final LayoutInflater inflater = LayoutInflater.from(activityTestRule.getActivity());
final TabLayout tabLayout = (TabLayout) inflater.inflate(R.layout.design_tabs, null);
final TabLayout.Tab tab = tabLayout.newTab();
tab.setCustomView(R.layout.design_tab_item_custom);
tabLayout.addTab(tab);
tab.setCustomView(R.layout.design_tab_item_custom_alternate);

assertNull(tabLayout.findViewById(R.id.my_custom_tab));
assertNotNull(tabLayout.findViewById(R.id.my_custom_alternate_tab));
}

/**
* Tests that the indicator animation still functions as intended when modifying the animator's
* update listener, instead of removing/recreating the animator itself.
Expand Down

0 comments on commit 3c0c9e7

Please sign in to comment.