Home » Android » android – TabLayout missing after updating Design Support Library

android – TabLayout missing after updating Design Support Library

Posted by: admin June 15, 2020 Leave a comment

Questions:

I updated the design support library from version 22.2.0 to 22.2.1 yesterday and I’m facing a strange behaviour with TabLayout.
On version 22.2.0, the TabLayout worked just fine, but now it doesn’t show up in my frag unless I rotate my phone (then it appears).
I haven’t changed my code, it just stopped working.

Here are the snippets:

public class FriendFragment extends Fragment {
  @Bind(R.id.friendPager)
  ViewPager viewPager;
  @Bind(R.id.friendSlideTab)
  TabLayout tabLayout;
  ...
  @Override
  public void onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
   View v = inflater.inflate(R.layout.fragment_friend,container,false);
   ButterKnife.bind(this,v);

   return v;
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    list.add(new SlideFragment(getString(R.string.my_friends), new MyFriendsFragment()));
    list.add(new SlideFragment(getString(R.string.add_friend), new SearchFriendFragment()));

    adapter = new FragmentSliderAdapter(list, getChildFragmentManager());
    viewPager.setAdapter(adapter);
    tabLayout.setupWithViewPager(viewPager);
}


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical">

    <android.support.design.widget.TabLayout
        android:id="@+id/friendSlideTab"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    </android.support.design.widget.TabLayout>

    <android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/friendPager"
        android:layout_width="match_parent"
        android:layout_height="0px"
        android:layout_weight="1" />

</LinearLayout>

I use ButterKnife, don’t think it would make any difference since in the previous version it was working with it.

Thanks and any help would be appreciated !

How to&Answers:

I submited a bug on Google code, but there is a workaround for the problem:
In my onViewCreated method,I added:

if (ViewCompat.isLaidOut(tabLayout)) {
    tabLayout.setupWithViewPager(viewPager);
} else {
    tabLayout.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
        @Override
        public void onLayoutChange(...) {
            tabLayout.setupWithViewPager(viewPager);

            tabLayout.removeOnLayoutChangeListener(this);
        }
    });
}

Answer:

Had the same issue and thought I was crazy about why a working TabLayout suddenly broke. Read the Issue on Google Code but found that the accepted solution is not working on non-Lollipop devices.

But another solution submitted in the issue thread worked for older APIs:
other solution

tabLayout.post(new Runnable() {
  @Override
   public void run() {
        tabLayout.setupWithViewPager(pager);
   }
});

Hopefully it will be fixed in a future release of the library.

EDIT (31/08/2015) : I have tested with the new v23 of the Support Design Library and it seems that it has been fixed (tested on Lollipop and KitKat). No need to spawn a thread now 🙂