Code example for ViewPager.OnPageChangeListener

Methods: getPaddingLeft, getWidth

0
		updatePositions(positionOffsetPixels);
	} 
	 
	void updatePositions(int positionOffsetPixels){
		int textWidth = mCurrent.getWidth() - mCurrent.getPaddingLeft() - mCurrent.getPaddingRight();
		int maxOffset = this.getWidth() / 2 - textWidth / 2 - mArrowPadding;
		if(positionOffsetPixels > 0){
			maxOffset -= this.getPaddingLeft();
			int offset = Math.min(positionOffsetPixels, maxOffset - 1);
			mCurrent.setPadding(0, 0, 2 * offset, 0);
			 
			// Move previous text out of the way. Slightly buggy. 
			/* 
			int overlapLeft = mPreviousGroup.getRight() - mCurrent.getLeft() + mArrowPadding; 
			mPreviousGroup.setPadding(0, 0, Math.max(0, overlapLeft), 0); 
			mNextGroup.setPadding(0, 0, 0, 0); 
			*/ 
		}else{ 
			maxOffset -= this.getPaddingRight();
			int offset = Math.max(positionOffsetPixels, -maxOffset);
			mCurrent.setPadding(-2 * offset, 0, 0, 0);
			 
			// Move next text out of the way. Slightly buggy. 
			/* 
			int overlapRight = mCurrent.getRight() - mNextGroup.getLeft() + mArrowPadding; 
			mNextGroup.setPadding(Math.max(0, overlapRight), 0, 0, 0); 
			mPreviousGroup.setPadding(0, 0, 0, 0); 
			*/ 
		} 
	} 
	 
	/** 
	 * Hide arrows if we can't scroll further 
	 *  
	 * @param position 
	 */ 
	void updateArrows(int position){
		if(mPrevArrow != null){
			mPrevArrow.setVisibility(position == 0 ? View.INVISIBLE : View.VISIBLE);
			mNextArrow.setVisibility(position == mSize - 1 ? View.INVISIBLE : View.VISIBLE);
		} 
	} 
	 
	/** 
	 * Adjust position to be the view that is showing the most. 
	 *  
	 * @param givenPosition 
	 * @param offset 
	 * @return 
	 */ 
	int updatePosition(int givenPosition, int offset){
		int pos;
		if(offset < 0){
			pos = givenPosition + 1;
		}else{ 
			pos = givenPosition;
		} 
		return pos; 
	} 
	 
	/** 
	 * Fade "currently showing" color depending on it's position 
	 *  
	 * @param offset 
	 */ 
	void updateColor(int offset){
		offset = Math.abs(offset);
		// Initial condition: offset is always 0, this.getWidth is also 0! 0/0 = NaN 
		int width = this.getWidth();
		float fraction = width == 0 ? 0 : offset / ((float)width / 4.0f);
		fraction = Math.min(1, fraction);
		int r = (int)(mUnfocusedTextColor[0] * fraction + mFocusedTextColor[0] * (1 - fraction));
		int g = (int)(mUnfocusedTextColor[1] * fraction + mFocusedTextColor[1] * (1 - fraction));
		int b = (int)(mUnfocusedTextColor[2] * fraction + mFocusedTextColor[2] * (1 - fraction));
		mCurrent.setTextColor(Color.argb(255, r, g, b));
	} 
	 
	/** 
	 * Update text depending on it's position 
	 *  
	 * @param prevPos 
	 */ 
	void setText(int prevPos){
		if(prevPos < 0){
			mPrevious.setText("");
		}else{ 
			mPrevious.setText(mPageInfoProvider.getTitle(prevPos));
		} 
		mCurrent.setText(mPageInfoProvider.getTitle(prevPos + 1));
		if(prevPos + 2 == this.mSize){
			mNext.setText("");
		}else{ 
			mNext.setText(mPageInfoProvider.getTitle(prevPos + 2));
		} 
	} 
	 
	// Original: 
	// 244, 245, 0, 1, 2 
	// New: 
	// -2, -1, 0, 1, 2 
	int adjustOffset(int positionOffsetPixels){
		// Move offset half width 
		positionOffsetPixels += this.getWidth() / 2;
		// Clamp to width 
		positionOffsetPixels %= this.getWidth();
		// Center around zero 
		positionOffsetPixels -= this.getWidth() / 2;
		return positionOffsetPixels;
	} 
	 
	@Override 
	public void onPageSelected(int position) {