TabLayout v23 不随 ViewPager 滚动而滚动
TabLayout v23 doesn't scroll with ViewPager scroll
在新的设计支持库 23.0.0 中,TabLayout
的选项卡在您拖动相关的 ViewPager
时不会滚动。仅当您完成拖动并向上单击时(当已选择选项卡时),选项卡才会滚动。这在 22.2.1
上运行良好
我是否必须添加更多内容才能使其像在 22.2.1 中一样工作?这是一个错误吗?有解决方法吗?
编辑
错误已在最新版本 23.0.1 中修复
Chris Banes (The main developer of the design support library). This issue was mentioned on the Google Issues page here, and solved here.
提出了解决该问题的方法
设置ViewPager
时输入此代码:
viewPager.clearOnPageChangeListeners();
viewPager.addOnPageChangeListener(new TabLayoutOnPageChangeListener(tabLayout));
还要添加这个 class:
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();
}
}
}
在新的设计支持库 23.0.0 中,TabLayout
的选项卡在您拖动相关的 ViewPager
时不会滚动。仅当您完成拖动并向上单击时(当已选择选项卡时),选项卡才会滚动。这在 22.2.1
我是否必须添加更多内容才能使其像在 22.2.1 中一样工作?这是一个错误吗?有解决方法吗?
编辑
错误已在最新版本 23.0.1 中修复
Chris Banes (The main developer of the design support library). This issue was mentioned on the Google Issues page here, and solved here.
提出了解决该问题的方法设置ViewPager
时输入此代码:
viewPager.clearOnPageChangeListeners();
viewPager.addOnPageChangeListener(new TabLayoutOnPageChangeListener(tabLayout));
还要添加这个 class:
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();
}
}
}