TabLayout v23不会使用ViewPager滚动滚动

时间:2015-08-27 12:54:02

标签: android androiddesignsupport android-tablayout

使用新的设计支持库23.0.0时,拖动TabLayout时,ViewPager的选项卡不会滚动。只有完成拖动并单击向上(选中选项卡时)才会滚动选项卡。这在22.2.1

上工作正常

我是否需要添加更多内容以使其像22.2.1一样工作?这是一个错误吗?有解决方法吗?

修改

使用上一版本23.0.1修复了错误

1 个答案:

答案 0 :(得分:1)

这是解决它的解决方法,由Chris Banes(设计支持库的主要开发人员)提出。 Google问题页面here上提到了此问题,并解决了here

设置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();
        }
    }
}