TabLayout missing after updating Design Support Library
Asked Answered
I

2

8

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 !

Inducement answered 21/7, 2015 at 13:9 Comment(8)
Have you seen: github.com/chrisbanes/cheesesquare? I am using a similar setup.Odelet
I've seen their code, it looks a lot like mine, but that don't solve my problem at all. Thanks again for the answer !Inducement
Your XML does not look his at all. Try using his example and slowing remove the extra views to match yours.Odelet
@JaredBurrows the only difference I can see is that the toolbar is in an appbarlayoutFlyleaf
@Flyleaf Provide your own suggestions.Odelet
Yeah, that would make no difference at all, since I don't want to add this behaviour to my Tabs.Inducement
did you update only com.android.support:design, or all of com.android.support?Flyleaf
I updated all, which in my case are com.android.support:appcompat, com.android.support:support-v4, com.android.support:recyclerview-v7, com.android.support:design, all to 22.2.1. ThanksInducement
I
7

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);
        }
    });
}
Inducement answered 22/7, 2015 at 18:45 Comment(2)
I had this problem too, your workaround works, thanks for that! Can you share a link to Google bug? I would like to follow it.Miscreance
I also have this issue with support library 23.1.1. Your workaround is not working for me.Us
A
2

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 :)

Asante answered 10/8, 2015 at 14:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.