更新Design Support Library后,TabLayout丢失

8
我昨天将设计支持库从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>

我使用ButterKnife,不认为这会有任何区别,因为在之前的版本中它与它一起工作。

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


@JaredBurrows 我能看到的唯一区别就是工具栏在一个AppBarLayout中。 - njzk2
@njzk2 请提供您自己的建议。 - Jared Burrows
是的,那样完全没有任何区别,因为我不想将此行为添加到我的选项卡中。 - Leonardo
你只更新了 com.android.support:design 还是所有的 com.android.support 都更新了? - njzk2
我已经将所有的库更新到了22.2.1版本,这些库包括com.android.support:appcompatcom.android.support:support-v4com.android.support:recyclerview-v7com.android.support:design。谢谢! - Leonardo
显示剩余3条评论
2个回答

7

我在Google Code上提交了一个bug,但是有一个解决方法:在我的onViewCreated方法中,我添加了以下内容:

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

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

我也遇到了这个问题,你的解决方法很有效,谢谢!你能分享一下 Google bug 的链接吗?我想要关注一下。 - Damian Petla
我也遇到了支持库23.1.1的这个问题。你的解决方法对我不起作用。 - user3749883

2

我曾经遇到过同样的问题,一直以为自己疯了,为什么一个正常工作的TabLayout突然就不行了。在Google Code上阅读了相关问题,但发现被认可的解决方案无法在非Lollipop设备上使用。

但在问题讨论中提交的另一个解决方案适用于旧版本API: 其他解决方案

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

希望在未来的库版本中能够解决这个问题。

编辑(2015年8月31日):我已经测试了支持设计库的新版本v23,似乎已经修复了这个问题(在Lollipop和KitKat上进行了测试)。现在不需要再创建线程了 :)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接