FileDocCategorySizeDatePackage
PagerTabStrip.javaAPI DocAndroid 5.1 API9953Thu Mar 12 22:22:56 GMT 2015android.support.v4.view

PagerTabStrip

public class PagerTabStrip extends PagerTitleStrip
PagerTabStrip is an interactive indicator of the current, next, and previous pages of a {@link ViewPager}. It is intended to be used as a child view of a ViewPager widget in your XML layout. Add it as a child of a ViewPager in your layout file and set its android:layout_gravity to TOP or BOTTOM to pin it to the top or bottom of the ViewPager. The title from each page is supplied by the method {@link PagerAdapter#getPageTitle(int)} in the adapter supplied to the ViewPager.

For a non-interactive indicator, see {@link PagerTitleStrip}.

Fields Summary
private static final String
TAG
private static final int
INDICATOR_HEIGHT
private static final int
MIN_PADDING_BOTTOM
private static final int
TAB_PADDING
private static final int
TAB_SPACING
private static final int
MIN_TEXT_SPACING
private static final int
FULL_UNDERLINE_HEIGHT
private static final int
MIN_STRIP_HEIGHT
private int
mIndicatorColor
private int
mIndicatorHeight
private int
mMinPaddingBottom
private int
mMinTextSpacing
private int
mMinStripHeight
private int
mTabPadding
private final android.graphics.Paint
mTabPaint
private final android.graphics.Rect
mTempRect
private int
mTabAlpha
private boolean
mDrawFullUnderline
private boolean
mDrawFullUnderlineSet
private int
mFullUnderlineHeight
private boolean
mIgnoreTap
private float
mInitialMotionX
private float
mInitialMotionY
private int
mTouchSlop
Constructors Summary
public PagerTabStrip(android.content.Context context)


       
        this(context, null);
    
public PagerTabStrip(android.content.Context context, android.util.AttributeSet attrs)

        super(context, attrs);

        mIndicatorColor = mTextColor;
        mTabPaint.setColor(mIndicatorColor);

        // Note: this follows the rules for Resources#getDimensionPixelOffset/Size:
        //       sizes round up, offsets round down.
        final float density = context.getResources().getDisplayMetrics().density;
        mIndicatorHeight = (int) (INDICATOR_HEIGHT * density + 0.5f);
        mMinPaddingBottom = (int) (MIN_PADDING_BOTTOM * density + 0.5f);
        mMinTextSpacing = (int) (MIN_TEXT_SPACING * density);
        mTabPadding = (int) (TAB_PADDING * density + 0.5f);
        mFullUnderlineHeight = (int) (FULL_UNDERLINE_HEIGHT * density + 0.5f);
        mMinStripHeight = (int) (MIN_STRIP_HEIGHT * density + 0.5f);
        mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();

        // Enforce restrictions
        setPadding(getPaddingLeft(), getPaddingTop(), getPaddingRight(), getPaddingBottom());
        setTextSpacing(getTextSpacing());

        setWillNotDraw(false);

        mPrevText.setFocusable(true);
        mPrevText.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mPager.setCurrentItem(mPager.getCurrentItem() - 1);
            }
        });

        mNextText.setFocusable(true);
        mNextText.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mPager.setCurrentItem(mPager.getCurrentItem() + 1);
            }
        });

        if (getBackground() == null) {
            mDrawFullUnderline = true;
        }
    
Methods Summary
public booleangetDrawFullUnderline()
Return whether or not this tab strip will draw a full-width underline. This defaults to true if no background is set.

return
true if this tab strip will draw a full-width underline in the current tab indicator color.

        return mDrawFullUnderline;
    
intgetMinHeight()

        return Math.max(super.getMinHeight(), mMinStripHeight);
    
public intgetTabIndicatorColor()

return
The current tab indicator color as an 0xRRGGBB value.

        return mIndicatorColor;
    
protected voidonDraw(android.graphics.Canvas canvas)

        super.onDraw(canvas);

        final int height = getHeight();
        final int bottom = height;
        final int left = mCurrText.getLeft() - mTabPadding;
        final int right = mCurrText.getRight() + mTabPadding;
        final int top = bottom - mIndicatorHeight;

        mTabPaint.setColor(mTabAlpha << 24 | (mIndicatorColor & 0xFFFFFF));
        canvas.drawRect(left, top, right, bottom, mTabPaint);

        if (mDrawFullUnderline) {
            mTabPaint.setColor(0xFF << 24 | (mIndicatorColor & 0xFFFFFF));
            canvas.drawRect(getPaddingLeft(), height - mFullUnderlineHeight,
                    getWidth() - getPaddingRight(), height, mTabPaint);
        }
    
public booleanonTouchEvent(android.view.MotionEvent ev)

        final int action = ev.getAction();
        if (action != MotionEvent.ACTION_DOWN && mIgnoreTap) {
            return false;
        }

        // Any tap within touch slop to either side of the current item
        // will scroll to prev/next.
        final float x = ev.getX();
        final float y = ev.getY();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                mInitialMotionX = x;
                mInitialMotionY = y;
                mIgnoreTap = false;
                break;

            case MotionEvent.ACTION_MOVE:
                if (Math.abs(x - mInitialMotionX) > mTouchSlop ||
                        Math.abs(y - mInitialMotionY) > mTouchSlop) {
                    mIgnoreTap = true;
                }
                break;

            case MotionEvent.ACTION_UP:
                if (x < mCurrText.getLeft() - mTabPadding) {
                    mPager.setCurrentItem(mPager.getCurrentItem() - 1);
                } else if (x > mCurrText.getRight() + mTabPadding) {
                    mPager.setCurrentItem(mPager.getCurrentItem() + 1);
                }
                break;
        }

        return true;
    
public voidsetBackgroundColor(int color)

        super.setBackgroundColor(color);
        if (!mDrawFullUnderlineSet) {
            mDrawFullUnderline = (color & 0xFF000000) == 0;
        }
    
public voidsetBackgroundDrawable(android.graphics.drawable.Drawable d)

        super.setBackgroundDrawable(d);
        if (!mDrawFullUnderlineSet) {
            mDrawFullUnderline = d == null;
        }
    
public voidsetBackgroundResource(int resId)

        super.setBackgroundResource(resId);
        if (!mDrawFullUnderlineSet) {
            mDrawFullUnderline = resId == 0;
        }
    
public voidsetDrawFullUnderline(boolean drawFull)
Set whether this tab strip should draw a full-width underline in the current tab indicator color.

param
drawFull true to draw a full-width underline, false otherwise

        mDrawFullUnderline = drawFull;
        mDrawFullUnderlineSet = true;
        invalidate();
    
public voidsetPadding(int left, int top, int right, int bottom)

        if (bottom < mMinPaddingBottom) {
            bottom = mMinPaddingBottom;
        }
        super.setPadding(left, top, right, bottom);
    
public voidsetTabIndicatorColor(int color)
Set the color of the tab indicator bar.

param
color Color to set as an 0xRRGGBB value. The high byte (alpha) is ignored.

        mIndicatorColor = color;
        mTabPaint.setColor(mIndicatorColor);
        invalidate();
    
public voidsetTabIndicatorColorResource(int resId)
Set the color of the tab indicator bar from a color resource.

param
resId Resource ID of a color resource to load

        setTabIndicatorColor(getContext().getResources().getColor(resId));
    
public voidsetTextSpacing(int textSpacing)

        if (textSpacing < mMinTextSpacing) {
            textSpacing = mMinTextSpacing;
        }
        super.setTextSpacing(textSpacing);
    
voidupdateTextPositions(int position, float positionOffset, boolean force)

        final Rect r = mTempRect;
        int bottom = getHeight();
        int left = mCurrText.getLeft() - mTabPadding;
        int right = mCurrText.getRight() + mTabPadding;
        int top = bottom - mIndicatorHeight;

        r.set(left, top, right, bottom);

        super.updateTextPositions(position, positionOffset, force);
        mTabAlpha = (int) (Math.abs(positionOffset - 0.5f) * 2 * 0xFF);

        left = mCurrText.getLeft() - mTabPadding;
        right = mCurrText.getRight() + mTabPadding;
        r.union(left, top, right, bottom);

        invalidate(r);