Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TabLayout missing after updating Design Support Library

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 !

like image 940
Leonardo Avatar asked Jul 21 '15 13:07

Leonardo


1 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);
        }
    });
}
like image 120
Leonardo Avatar answered Oct 21 '22 04:10

Leonardo