更新设计支持库后缺少TabLayout

Leo*_*rdo 8 android android-layout android-support-library butterknife android-tablayout

我昨天将设计支持库从版本22.2.0更新到22.2.1,我遇到了一个奇怪的行为TabLayout.在版本22.2.0上,TabLayout工作得很好,但现在它没有显示在我的碎片中,除非我旋转手机(然后它出现).我没有改变我的代码,它只是停止了工作.

以下是片段:

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>
Run Code Online (Sandbox Code Playgroud)

我使用ButterKnife,不要认为它会有任何区别,因为在之前的版本中它正在使用它.

谢谢,任何帮助将不胜感激!

Leo*_*rdo 7

我提交了一个关于Google代码的错误,但有一个问题的解决方法:在我的onViewCreated方法中,我添加了:

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

            tabLayout.removeOnLayoutChangeListener(this);
        }
    });
}
Run Code Online (Sandbox Code Playgroud)

  • Google resport:https://code.google.com/p/android/issues/detail?id = 180462 (2认同)