1

使用新的设计支持库 23.0.0,在TabLayout您拖动关联的时不会滚动选项卡ViewPager。仅当您完成拖动并向上单击时(当已选择选项卡时),选项卡才会滚动。这在 22.2.1 上运行良好

我是否必须添加更多内容才能使其像在 22.2.1 中一样工作?它是一个错误吗?有解决方法吗?

编辑

上一个版本 23.0.1 修复的错误

4

1 回答 1

1

这是Chris Banes(设计支持库的主要开发人员)提出的解决方法。此问题已在此处的 Google 问题页面上提及,并已在此处解决。

设置时输入此代码ViewPager

    viewPager.clearOnPageChangeListeners();
    viewPager.addOnPageChangeListener(new TabLayoutOnPageChangeListener(tabLayout));

还要添加这个类:

private static class TabLayoutOnPageChangeListener implements ViewPager.OnPageChangeListener {

    private final WeakReference<TabLayout> mTabLayoutRef;
    private int mPreviousScrollState;
    private int mScrollState;

    public TabLayoutOnPageChangeListener(TabLayout tabLayout) {
        mTabLayoutRef = new WeakReference<>(tabLayout);
    }

    @Override
    public void onPageScrollStateChanged(int state) {
        mPreviousScrollState = mScrollState;
        mScrollState = state;
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        final TabLayout tabLayout = mTabLayoutRef.get();
        if (tabLayout != null) {
            final boolean updateText = (mScrollState == ViewPager.SCROLL_STATE_DRAGGING)
                    || (mScrollState == ViewPager.SCROLL_STATE_SETTLING
                    && mPreviousScrollState == ViewPager.SCROLL_STATE_DRAGGING);
            tabLayout.setScrollPosition(position, positionOffset, updateText);
        }
    }

    @Override
    public void onPageSelected(int position) {
        final TabLayout tabLayout = mTabLayoutRef.get();
        if (tabLayout != null) {
            tabLayout.getTabAt(position).select();
        }
    }
}
于 2015-09-01T12:23:47.867 回答