void updateAdapter(PagerAdapter oldAdapter, PagerAdapter newAdapter) { if (oldAdapter != null) { oldAdapter.unregisterDataSetObserver(mPageListener); } if (newAdapter != null) { newAdapter.registerDataSetObserver(mPageListener); } if (mPager != null) { mLastKnownCurrentPage = -1; mLastKnownPositionOffset = -1; updateText(mPager.getCurrentItem(), newAdapter); requestLayout(); } }
void updateTextPositions(int position, float positionOffset) { if (position != mLastKnownCurrentPage) { updateText(position, mPager.getAdapter()); } else if (positionOffset == mLastKnownPositionOffset) { return; } mUpdatingPositions = true; final int prevWidth = mPrevText.getMeasuredWidth(); final int currWidth = mCurrText.getMeasuredWidth(); final int nextWidth = mNextText.getMeasuredWidth(); final int halfCurrWidth = currWidth / 2; final int stripWidth = getWidth(); final int paddingLeft = getPaddingLeft(); final int paddingRight = getPaddingRight(); final int paddingTop = getPaddingTop(); final int textPaddedLeft = paddingLeft + halfCurrWidth; final int textPaddedRight = paddingRight + halfCurrWidth; final int contentWidth = stripWidth - textPaddedLeft - textPaddedRight; float currOffset = positionOffset + 0.5f; if (currOffset > 1.f) { currOffset -= 1.f; } final int currCenter = stripWidth - textPaddedRight - (int) (contentWidth * currOffset); final int currLeft = currCenter - currWidth / 2; final int currRight = currLeft + currWidth; mCurrText.layout(currLeft, paddingTop, currRight, paddingTop + mCurrText.getMeasuredHeight()); final int prevLeft = Math.min(paddingLeft, currLeft - mScaledTextSpacing - prevWidth); mPrevText.layout( prevLeft, paddingTop, prevLeft + prevWidth, paddingTop + mPrevText.getMeasuredHeight()); final int nextLeft = Math.max(stripWidth - paddingRight - nextWidth, currRight + mScaledTextSpacing); mNextText.layout( nextLeft, paddingTop, nextLeft + nextWidth, paddingTop + mNextText.getMeasuredHeight()); mLastKnownPositionOffset = positionOffset; mUpdatingPositions = false; }