FileDocCategorySizeDatePackage
HorizontalGridView.javaAPI DocAndroid 5.1 API13993Thu Mar 12 22:22:56 GMT 2015android.support.v17.leanback.widget

HorizontalGridView

public class HorizontalGridView extends BaseGridView
A view that shows items in a horizontal scrolling list. The items come from the {@link RecyclerView.Adapter} associated with this view.

Fields Summary
private boolean
mFadingLowEdge
private boolean
mFadingHighEdge
private android.graphics.Paint
mTempPaint
private android.graphics.Bitmap
mTempBitmapLow
private android.graphics.LinearGradient
mLowFadeShader
private int
mLowFadeShaderLength
private int
mLowFadeShaderOffset
private android.graphics.Bitmap
mTempBitmapHigh
private android.graphics.LinearGradient
mHighFadeShader
private int
mHighFadeShaderLength
private int
mHighFadeShaderOffset
private android.graphics.Rect
mTempRect
Constructors Summary
public HorizontalGridView(android.content.Context context)


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

        this(context, attrs, 0);
    
public HorizontalGridView(android.content.Context context, android.util.AttributeSet attrs, int defStyle)

        super(context, attrs, defStyle);
        mLayoutManager.setOrientation(RecyclerView.HORIZONTAL);
        initAttributes(context, attrs);
    
Methods Summary
public voiddraw(android.graphics.Canvas canvas)

        final boolean needsFadingLow = needsFadingLowEdge();
        final boolean needsFadingHigh = needsFadingHighEdge();
        if (!needsFadingLow) {
            mTempBitmapLow = null;
        }
        if (!needsFadingHigh) {
            mTempBitmapHigh = null;
        }
        if (!needsFadingLow && !needsFadingHigh) {
            super.draw(canvas);
            return;
        }

        int lowEdge = mFadingLowEdge? getPaddingLeft() - mLowFadeShaderOffset - mLowFadeShaderLength : 0;
        int highEdge = mFadingHighEdge ? getWidth() - getPaddingRight()
                + mHighFadeShaderOffset + mHighFadeShaderLength : getWidth();

        // draw not-fade content
        int save = canvas.save();
        canvas.clipRect(lowEdge + (mFadingLowEdge ? mLowFadeShaderLength : 0), 0,
                highEdge - (mFadingHighEdge ? mHighFadeShaderLength : 0), getHeight());
        super.draw(canvas);
        canvas.restoreToCount(save);

        Canvas tmpCanvas = new Canvas();
        mTempRect.top = 0;
        mTempRect.bottom = getHeight();
        if (needsFadingLow && mLowFadeShaderLength > 0) {
            Bitmap tempBitmap = getTempBitmapLow();
            tempBitmap.eraseColor(Color.TRANSPARENT);
            tmpCanvas.setBitmap(tempBitmap);
            // draw original content
            int tmpSave = tmpCanvas.save();
            tmpCanvas.clipRect(0, 0, mLowFadeShaderLength, getHeight());
            tmpCanvas.translate(-lowEdge, 0);
            super.draw(tmpCanvas);
            tmpCanvas.restoreToCount(tmpSave);
            // draw fading out
            mTempPaint.setShader(mLowFadeShader);
            tmpCanvas.drawRect(0, 0, mLowFadeShaderLength, getHeight(), mTempPaint);
            // copy back to canvas
            mTempRect.left = 0;
            mTempRect.right = mLowFadeShaderLength;
            canvas.translate(lowEdge, 0);
            canvas.drawBitmap(tempBitmap, mTempRect, mTempRect, null);
            canvas.translate(-lowEdge, 0);
        }
        if (needsFadingHigh && mHighFadeShaderLength > 0) {
            Bitmap tempBitmap = getTempBitmapHigh();
            tempBitmap.eraseColor(Color.TRANSPARENT);
            tmpCanvas.setBitmap(tempBitmap);
            // draw original content
            int tmpSave = tmpCanvas.save();
            tmpCanvas.clipRect(0, 0, mHighFadeShaderLength, getHeight());
            tmpCanvas.translate(-(highEdge - mHighFadeShaderLength), 0);
            super.draw(tmpCanvas);
            tmpCanvas.restoreToCount(tmpSave);
            // draw fading out
            mTempPaint.setShader(mHighFadeShader);
            tmpCanvas.drawRect(0, 0, mHighFadeShaderLength, getHeight(), mTempPaint);
            // copy back to canvas
            mTempRect.left = 0;
            mTempRect.right = mHighFadeShaderLength;
            canvas.translate(highEdge - mHighFadeShaderLength, 0);
            canvas.drawBitmap(tempBitmap, mTempRect, mTempRect, null);
            canvas.translate(-(highEdge - mHighFadeShaderLength), 0);
        }
    
public final booleangetFadingLeftEdge()
Return true if fading left edge.

        return mFadingLowEdge;
    
public final intgetFadingLeftEdgeLength()
Get left edge fading length in pixels.

        return mLowFadeShaderLength;
    
public final intgetFadingLeftEdgeOffset()
Get distance in pixels between fading start position and left padding edge. The fading start position is positive when start position is inside left padding area. Default value is 0, means that the fading starts from left padding edge.

        return mLowFadeShaderOffset;
    
public final booleangetFadingRightEdge()
Return true if fading right edge.

        return mFadingHighEdge;
    
public final intgetFadingRightEdgeLength()
Get right edge fading length in pixels.

        return mHighFadeShaderLength;
    
public final intgetFadingRightEdgeOffset()
Set distance in pixels between fading start position and right padding edge. The fading start position is positive when start position is inside right padding area. Default value is 0, means that the fading starts from right padding edge.

        return mHighFadeShaderOffset;
    
private android.graphics.BitmapgetTempBitmapHigh()

        if (mTempBitmapHigh == null
                || mTempBitmapHigh.getWidth() != mHighFadeShaderLength
                || mTempBitmapHigh.getHeight() != getHeight()) {
            // TODO: fix logic for sharing mTempBitmapLow
            if (false && mTempBitmapLow != null
                    && mTempBitmapLow.getWidth() == mHighFadeShaderLength
                    && mTempBitmapLow.getHeight() == getHeight()) {
                // share same bitmap for low edge fading and high edge fading.
                mTempBitmapHigh = mTempBitmapLow;
            } else {
                mTempBitmapHigh = Bitmap.createBitmap(mHighFadeShaderLength, getHeight(),
                        Bitmap.Config.ARGB_8888);
            }
        }
        return mTempBitmapHigh;
    
private android.graphics.BitmapgetTempBitmapLow()

        if (mTempBitmapLow == null
                || mTempBitmapLow.getWidth() != mLowFadeShaderLength
                || mTempBitmapLow.getHeight() != getHeight()) {
            mTempBitmapLow = Bitmap.createBitmap(mLowFadeShaderLength, getHeight(),
                    Bitmap.Config.ARGB_8888);
        }
        return mTempBitmapLow;
    
protected voidinitAttributes(android.content.Context context, android.util.AttributeSet attrs)

        initBaseGridViewAttributes(context, attrs);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.lbHorizontalGridView);
        setRowHeight(a);
        setNumRows(a.getInt(R.styleable.lbHorizontalGridView_numberOfRows, 1));
        a.recycle();
        updateLayerType();
        mTempPaint = new Paint();
        mTempPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
    
private booleanneedsFadingHighEdge()

        if (!mFadingHighEdge) {
            return false;
        }
        final int c = getChildCount();
        for (int i = c - 1; i >= 0; i--) {
            View view = getChildAt(i);
            if (mLayoutManager.getOpticalRight(view) > getWidth()
                    - getPaddingRight() + mHighFadeShaderOffset) {
                return true;
            }
        }
        return false;
    
private booleanneedsFadingLowEdge()

        if (!mFadingLowEdge) {
            return false;
        }
        final int c = getChildCount();
        for (int i = 0; i < c; i++) {
            View view = getChildAt(i);
            if (mLayoutManager.getOpticalLeft(view) <
                    getPaddingLeft() - mLowFadeShaderOffset) {
                return true;
            }
        }
        return false;
    
public final voidsetFadingLeftEdge(boolean fading)
Set fade out left edge to transparent. Note turn on fading edge is very expensive that you should turn off when HorizontalGridView is scrolling.

        if (mFadingLowEdge != fading) {
            mFadingLowEdge = fading;
            if (!mFadingLowEdge) {
                mTempBitmapLow = null;
            }
            invalidate();
            updateLayerType();
        }
    
public final voidsetFadingLeftEdgeLength(int fadeLength)
Set left edge fading length in pixels.

        if (mLowFadeShaderLength != fadeLength) {
            mLowFadeShaderLength = fadeLength;
            if (mLowFadeShaderLength != 0) {
                mLowFadeShader = new LinearGradient(0, 0, mLowFadeShaderLength, 0,
                        Color.TRANSPARENT, Color.BLACK, Shader.TileMode.CLAMP);
            } else {
                mLowFadeShader = null;
            }
            invalidate();
        }
    
public final voidsetFadingLeftEdgeOffset(int fadeOffset)
Set distance in pixels between fading start position and left padding edge. The fading start position is positive when start position is inside left padding area. Default value is 0, means that the fading starts from left padding edge.

        if (mLowFadeShaderOffset != fadeOffset) {
            mLowFadeShaderOffset = fadeOffset;
            invalidate();
        }
    
public final voidsetFadingRightEdge(boolean fading)
Set fade out right edge to transparent. Note turn on fading edge is very expensive that you should turn off when HorizontalGridView is scrolling.

        if (mFadingHighEdge != fading) {
            mFadingHighEdge = fading;
            if (!mFadingHighEdge) {
                mTempBitmapHigh = null;
            }
            invalidate();
            updateLayerType();
        }
    
public final voidsetFadingRightEdgeLength(int fadeLength)
Set right edge fading length in pixels.

        if (mHighFadeShaderLength != fadeLength) {
            mHighFadeShaderLength = fadeLength;
            if (mHighFadeShaderLength != 0) {
                mHighFadeShader = new LinearGradient(0, 0, mHighFadeShaderLength, 0,
                        Color.BLACK, Color.TRANSPARENT, Shader.TileMode.CLAMP);
            } else {
                mHighFadeShader = null;
            }
            invalidate();
        }
    
public final voidsetFadingRightEdgeOffset(int fadeOffset)
Get distance in pixels between fading start position and right padding edge. The fading start position is positive when start position is inside right padding area. Default value is 0, means that the fading starts from right padding edge.

        if (mHighFadeShaderOffset != fadeOffset) {
            mHighFadeShaderOffset = fadeOffset;
            invalidate();
        }
    
public voidsetNumRows(int numRows)
Set the number of rows. Defaults to one.

        mLayoutManager.setNumRows(numRows);
        requestLayout();
    
voidsetRowHeight(android.content.res.TypedArray array)

        TypedValue typedValue = array.peekValue(R.styleable.lbHorizontalGridView_rowHeight);
        int size;
        if (typedValue != null && typedValue.type == TypedValue.TYPE_DIMENSION) {
            size = array.getDimensionPixelSize(R.styleable.lbHorizontalGridView_rowHeight, 0);
        } else {
            size = array.getInt(R.styleable.lbHorizontalGridView_rowHeight, 0);
        }
        setRowHeight(size);
    
public voidsetRowHeight(int height)
Set the row height.

param
height May be WRAP_CONTENT, or a size in pixels. If zero, row height will be fixed based on number of rows and view height.

        mLayoutManager.setRowHeight(height);
        requestLayout();
    
private voidupdateLayerType()
Updates the layer type for this view. If fading edges are needed, use a hardware layer. This works around the problem that when a child invalidates itself (for example has an animated background), the parent view must also be invalidated to refresh the display list which updates the the caching bitmaps used to draw the fading edges.

        if (mFadingLowEdge || mFadingHighEdge) {
            setLayerType(View.LAYER_TYPE_HARDWARE, null);
            setWillNotDraw(false);
        } else {
            setLayerType(View.LAYER_TYPE_NONE, null);
            setWillNotDraw(true);
        }