@Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); if (tabStrip.isIndicatorAlwaysInCenter() && tabStrip.getChildCount() > 0) { View firstTab = tabStrip.getChildAt(0); View lastTab = tabStrip.getChildAt(getChildCount() - 1); int start = (w - Utils.getMeasuredWidth(firstTab)) / 2 - Utils.getMarginStart(firstTab); int end = (w - Utils.getMeasuredWidth(lastTab)) / 2 - Utils.getMarginEnd(lastTab); tabStrip.setMinimumWidth(tabStrip.getMeasuredWidth()); ViewCompat.setPaddingRelative(this, start, getPaddingTop(), end, getPaddingBottom()); setClipToPadding(false); } }
private void scrollToTab(int tabIndex, int positionOffset) { final int tabStripChildCount = mTabStrip.getChildCount(); if (tabStripChildCount == 0 || tabIndex < 0 || tabIndex >= tabStripChildCount) { return; } View selectedChild = mTabStrip.getChildAt(tabIndex); if (selectedChild != null) { int targetScrollX = selectedChild.getLeft() + positionOffset; if (mTabStrip.isIndicatorAlwaysInCenter()) { targetScrollX -= (mTabStrip.getChildWidthAt(0) - selectedChild.getWidth()) / 2; } else if (tabIndex > 0 || positionOffset > 0) { // If we're not at the first child and are mid-scroll, make sure we obey the offset targetScrollX -= mTitleOffset; } scrollTo(targetScrollX, 0); } }
private void scrollToTab(int tabIndex, float positionOffset) { final int tabStripChildCount = tabStrip.getChildCount(); if (tabStripChildCount == 0 || tabIndex < 0 || tabIndex >= tabStripChildCount) { return; } final boolean isLayoutRtl = Utils.isLayoutRtl(this); View selectedTab = tabStrip.getChildAt(tabIndex); int widthPlusMargin = Utils.getWidth(selectedTab) + Utils.getMarginHorizontally(selectedTab); int extraOffset = (int) (positionOffset * widthPlusMargin); if (tabStrip.isIndicatorAlwaysInCenter()) { if (0f < positionOffset && positionOffset < 1f) { View nextTab = tabStrip.getChildAt(tabIndex + 1); int selectHalfWidth = Utils.getWidth(selectedTab) / 2 + Utils.getMarginEnd(selectedTab); int nextHalfWidth = Utils.getWidth(nextTab) / 2 + Utils.getMarginStart(nextTab); extraOffset = Math.round(positionOffset * (selectHalfWidth + nextHalfWidth)); } View firstTab = tabStrip.getChildAt(0); int x; if (isLayoutRtl) { int first = Utils.getWidth(firstTab) + Utils.getMarginEnd(firstTab); int selected = Utils.getWidth(selectedTab) + Utils.getMarginEnd(selectedTab); x = Utils.getEnd(selectedTab) - Utils.getMarginEnd(selectedTab) - extraOffset; x -= (first - selected) / 2; } else { int first = Utils.getWidth(firstTab) + Utils.getMarginStart(firstTab); int selected = Utils.getWidth(selectedTab) + Utils.getMarginStart(selectedTab); x = Utils.getStart(selectedTab) - Utils.getMarginStart(selectedTab) + extraOffset; x -= (first - selected) / 2; } scrollTo(x, 0); return; } int x; if (titleOffset == TITLE_OFFSET_AUTO_CENTER) { if (0f < positionOffset && positionOffset < 1f) { View nextTab = tabStrip.getChildAt(tabIndex + 1); int selectHalfWidth = Utils.getWidth(selectedTab) / 2 + Utils.getMarginEnd(selectedTab); int nextHalfWidth = Utils.getWidth(nextTab) / 2 + Utils.getMarginStart(nextTab); extraOffset = Math.round(positionOffset * (selectHalfWidth + nextHalfWidth)); } if (isLayoutRtl) { x = -Utils.getWidthWithMargin(selectedTab) / 2 + getWidth() / 2; } else { x = Utils.getWidthWithMargin(selectedTab) / 2 - getWidth() / 2; } } else { if (isLayoutRtl) { x = (tabIndex > 0 || positionOffset > 0) ? titleOffset : 0; } else { x = (tabIndex > 0 || positionOffset > 0) ? -titleOffset : 0; } } int start = Utils.getStart(selectedTab); int startMargin = Utils.getMarginStart(selectedTab); if (isLayoutRtl) { x += start + startMargin - extraOffset - getWidth() + Utils.getPaddingHorizontally(this); } else { x += start - startMargin + extraOffset; } scrollTo(x, 0); }