Example usage for android.view View getParent

List of usage examples for android.view View getParent

Introduction

In this page you can find the example usage for android.view View getParent.

Prototype

public final ViewParent getParent() 

Source Link

Document

Gets the parent of this view.

Usage

From source file:ac.robinson.paperchains.PaperChainsActivity.java

private void animateRecordingInterface(int direction, View keyView) {
    mPlayButton.setVisibility(View.VISIBLE);
    mDeleteButton.setVisibility(View.VISIBLE);
    mSaveButton.setVisibility(View.VISIBLE);

    // animate the control buttons out to be equally spaced around the record button
    float buttonOffset = -mPlayButton.getWidth();
    PointF centre = new PointF(0, 0);
    PointF startingPoint = new PointF(0, buttonOffset);
    double radRot = Math.toRadians(-120);
    double cosRot = Math.cos(radRot);
    double sinRot = Math.sin(radRot);
    QRImageParser.rotatePoint(startingPoint, centre, cosRot, sinRot);
    float leftX = startingPoint.x;
    float leftY = startingPoint.y;
    QRImageParser.rotatePoint(startingPoint, centre, cosRot, sinRot);
    float rightX = startingPoint.x;
    float rightY = startingPoint.y;

    RelativeLayout parent;//from  w w  w  .  j  a v a2 s. com
    AnimatorSet buttonAnimator = new AnimatorSet();
    switch (direction) {
    case 1: // out
        // on an outward animation, we want the three minor buttons to have priority so the record button's
        // larger click area doesn't capture their clicks
        mPlayButton.bringToFront();
        mDeleteButton.bringToFront();
        mSaveButton.bringToFront();
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
            // need to manually re-layout before KitKat
            parent = (RelativeLayout) mPlayButton.getParent();
            parent.requestLayout();
            parent.invalidate();
        }

        buttonAnimator.playTogether(ObjectAnimator.ofFloat(mDeleteButton, "translationX", 0, leftX),
                ObjectAnimator.ofFloat(mDeleteButton, "translationY", 0, leftY),
                ObjectAnimator.ofFloat(mSaveButton, "translationX", 0, rightX),
                ObjectAnimator.ofFloat(mSaveButton, "translationY", 0, rightY),
                ObjectAnimator.ofFloat(mPlayButton, "translationY", 0, buttonOffset));
        buttonAnimator.setInterpolator(new OvershootInterpolator());
        break;
    case -1: // in
        // keyView is the view we want to be at the front after an inward animation
        if (keyView != null) {
            keyView.bringToFront();
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
                // need to manually re-layout before KitKat
                parent = (RelativeLayout) keyView.getParent();
                parent.requestLayout();
                parent.invalidate();
            }
        }

        buttonAnimator.playTogether(ObjectAnimator.ofFloat(mDeleteButton, "translationX", leftX, 0),
                ObjectAnimator.ofFloat(mDeleteButton, "translationY", leftY, 0),
                ObjectAnimator.ofFloat(mSaveButton, "translationX", rightX, 0),
                ObjectAnimator.ofFloat(mSaveButton, "translationY", rightY, 0),
                ObjectAnimator.ofFloat(mPlayButton, "translationY", buttonOffset, 0));
        buttonAnimator.setInterpolator(new AnticipateInterpolator());
        break;
    }
    buttonAnimator.setDuration(BUTTON_ANIMATION_DURATION);
    buttonAnimator.start();
}

From source file:com.bolaa.medical.view.pulltorefreshgrid.StaggeredGridView.java

/**
 * Should be called with mPopulating set to true
 *
 * @param fromPosition Position to start filling from
 * @param overhang     the number of extra pixels to fill beyond the current bottom edge
 * @return the max overhang beyond the end of the view of any added items at the bottom
 *///from   w w  w.j a va2s. c  om
final int fillDown(int fromPosition, int overhang) {

    final int paddingLeft = getPaddingLeft();
    final int paddingRight = getPaddingRight();
    final int itemMargin = mItemMargin;
    final int colWidth = (getWidth() - paddingLeft - paddingRight - itemMargin * (mColCount - 1)) / mColCount;
    final int gridBottom = getHeight() - getPaddingBottom();
    final int fillTo = gridBottom + overhang;
    int nextCol = getNextColumnDown(fromPosition);
    int position = fromPosition;

    while (nextCol >= 0 && mItemBottoms[nextCol] < fillTo && position < mItemCount) {

        final View child = obtainView(position, null);

        if (child == null)
            continue;

        LayoutParams lp = (LayoutParams) child.getLayoutParams();
        if (lp == null) {
            lp = this.generateDefaultLayoutParams();
            child.setLayoutParams(lp);
        }

        if (child.getParent() != this) {
            if (mInLayout) {
                addViewInLayout(child, -1, lp);
            } else {
                ViewParent parent = child.getParent();
                if (parent != null && parent instanceof ViewGroup) {
                    ((ViewGroup) parent).removeView(child);
                }
                addView(child);
            }
        }

        final int span = Math.min(mColCount, lp.span);
        final int widthSize = colWidth * span + itemMargin * (span - 1);
        final int widthSpec = MeasureSpec.makeMeasureSpec(widthSize, MeasureSpec.EXACTLY);

        LayoutRecord rec;
        if (span > 1) {
            rec = getNextRecordDown(position, span);
            //                nextCol = rec.column;
            nextCol = 0;
        } else {
            rec = mLayoutRecords.get(position);
        }

        boolean invalidateAfter = false;
        if (rec == null) {
            rec = new LayoutRecord();
            mLayoutRecords.put(position, rec);
            rec.column = nextCol;
            rec.span = span;
        } else if (span != rec.span) {
            rec.span = span;
            rec.column = nextCol;
            invalidateAfter = true;
        } else {
            //                nextCol = rec.column;
        }

        if (mHasStableIds) {
            final long id = mAdapter.getItemId(position);
            rec.id = id;
            lp.id = id;
        }

        lp.column = nextCol;

        /**
         * Magic does not exist
         */
        //            child.measure(MeasureSpec.EXACTLY, MeasureSpec.UNSPECIFIED);

        final int heightSpec;
        if (lp.height == LayoutParams.WRAP_CONTENT) {
            heightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
        } else {
            heightSpec = MeasureSpec.makeMeasureSpec(lp.height, MeasureSpec.EXACTLY);
        }
        child.measure(widthSpec, heightSpec);

        final int childHeight = child.getMeasuredHeight();
        if (invalidateAfter || (childHeight != rec.height && rec.height > 0)) {
            invalidateLayoutRecordsAfterPosition(position);
        }
        rec.height = childHeight;

        final int startFrom;
        if (span > 1) {
            int lowest = mItemBottoms[nextCol];

            //                final int colEnd = Math.min(mColCount, nextCol + lp.span);
            // Only for span = maxCol
            for (int i = 0; i < mColCount; i++) {
                final int bottom = mItemBottoms[i];
                if (bottom > lowest) {
                    lowest = bottom;
                }
            }
            startFrom = lowest;
        } else {
            startFrom = mItemBottoms[nextCol];
        }

        final int childTop = startFrom + itemMargin;
        final int childBottom = childTop + childHeight;
        final int childLeft;
        if (span > 1) {
            childLeft = paddingLeft;
        } else {
            childLeft = paddingLeft + nextCol * (colWidth + itemMargin);
        }
        final int childRight = childLeft + child.getMeasuredWidth();
        child.layout(childLeft, childTop, childRight, childBottom);

        rec.left = childLeft;
        rec.top = childTop;
        rec.right = childRight;
        rec.bottom = childBottom;
        rec.hasRecRecord = true;

        // add the position to the mapping
        if (!mColMappings.get(nextCol).contains(position)) {

            // check to see if the mapping exists in other columns
            // this would happen if list has been updated
            for (ArrayList<Integer> list : mColMappings) {
                if (list.contains(position)) {
                    list.remove((Integer) position);
                }
            }
            mColMappings.get(nextCol).add(position);
        }

        final int colEnd = Math.min(mColCount, nextCol + lp.span);
        for (int i = nextCol; i < colEnd; i++) {
            mItemBottoms[i] = childBottom + rec.getMarginBelow(i - nextCol);
        }

        position++;
        nextCol = getNextColumnDown(position);
    }

    int lowestView = 0;
    for (int i = 0; i < mColCount; i++) {
        if (mItemBottoms[i] > lowestView) {
            lowestView = mItemBottoms[i];
        }
    }

    return lowestView - gridBottom;
}

From source file:cn.iterlog.myapplication.widget.overscroll.StaggeredGridView.java

/**
 * Obtain a populated view from the adapter. If optScrap is non-null and is not
 * reused it will be placed in the recycle bin.
 *
 * @param position position to get view for
 * @param optScrap Optional scrap view; will be reused if possible
 * @return A new view, a recycled view from mRecycler, or optScrap
 *///from   ww  w .j av a2s.c  o  m
final View obtainView(int position, View optScrap) {
    View view = mRecycler.getTransientStateView(position);
    if (view != null) {
        return view;
    }

    if (position >= mAdapter.getCount()) {
        view = null;
        return null;
    }

    // Reuse optScrap if it's of the right type (and not null)
    final int optType = optScrap != null ? ((LayoutParams) optScrap.getLayoutParams()).viewType : -1;
    final int positionViewType = mAdapter.getItemViewType(position);
    final View scrap = optType == positionViewType ? optScrap : mRecycler.getScrapView(positionViewType);

    view = mAdapter.getView(position, scrap, this);

    if (view != scrap && scrap != null) {
        // The adapter didn't use it; put it back.
        mRecycler.addScrap(scrap);
    }

    ViewGroup.LayoutParams lp = view.getLayoutParams();

    if (view.getParent() != this) {
        if (lp == null) {
            lp = generateDefaultLayoutParams();
        } else if (!checkLayoutParams(lp)) {
            lp = generateLayoutParams(lp);
        }
    }

    final LayoutParams sglp = (LayoutParams) lp;
    sglp.position = position;
    sglp.viewType = positionViewType;

    //Set the updated LayoutParam before returning the view.
    view.setLayoutParams(sglp);
    return view;
}

From source file:cn.androidy.materialdesignsample.ryanharterviewpager.ViewPager.java

public boolean arrowScroll(int direction) {
    View currentFocused = findFocus();
    if (currentFocused == this) {
        currentFocused = null;//from  w w  w .  jav  a 2s . c  o  m
    } else if (currentFocused != null) {
        boolean isChild = false;
        for (ViewParent parent = currentFocused.getParent(); parent instanceof ViewGroup; parent = parent
                .getParent()) {
            if (parent == this) {
                isChild = true;
                break;
            }
        }
        if (!isChild) {
            // This would cause the focus search down below to fail in fun ways.
            final StringBuilder sb = new StringBuilder();
            sb.append(currentFocused.getClass().getSimpleName());
            for (ViewParent parent = currentFocused.getParent(); parent instanceof ViewGroup; parent = parent
                    .getParent()) {
                sb.append(" => ").append(parent.getClass().getSimpleName());
            }
            Log.e(TAG, "arrowScroll tried to find focus based on non-child " + "current focused view "
                    + sb.toString());
            currentFocused = null;
        }
    }

    boolean handled = false;

    View nextFocused = FocusFinder.getInstance().findNextFocus(this, currentFocused, direction);
    if (nextFocused != null && nextFocused != currentFocused) {
        if (direction == View.FOCUS_LEFT) {
            // If there is nothing to the left, or this is causing us to
            // jump to the right, then what we really want to do is page left.
            final int nextLeft = getChildRectInPagerCoordinates(mTempRect, nextFocused).left;
            final int currLeft = getChildRectInPagerCoordinates(mTempRect, currentFocused).left;
            if (currentFocused != null && nextLeft >= currLeft) {
                handled = pageLeft();
            } else {
                handled = nextFocused.requestFocus();
            }
        } else if (direction == View.FOCUS_RIGHT) {
            // If there is nothing to the right, or this is causing us to
            // jump to the left, then what we really want to do is page right.
            final int nextLeft = getChildRectInPagerCoordinates(mTempRect, nextFocused).left;
            final int currLeft = getChildRectInPagerCoordinates(mTempRect, currentFocused).left;
            if (currentFocused != null && nextLeft <= currLeft) {
                handled = pageRight();
            } else {
                handled = nextFocused.requestFocus();
            }
        } else if (direction == View.FOCUS_UP) {
            final int nextUp = getChildRectInPagerCoordinates(mTempRect, nextFocused).top;
            final int currUp = getChildRectInPagerCoordinates(mTempRect, currentFocused).top;
            if (currentFocused != null && nextUp >= currUp) {
                handled = pageLeft();
            } else {
                handled = nextFocused.requestFocus();
            }
        } else if (direction == View.FOCUS_DOWN) {
            final int nextUp = getChildRectInPagerCoordinates(mTempRect, nextFocused).top;
            final int currUp = getChildRectInPagerCoordinates(mTempRect, currentFocused).top;
            if (currentFocused != null && nextUp <= currUp) {
                handled = pageRight();
            } else {
                handled = nextFocused.requestFocus();
            }
        }
    } else if (direction == FOCUS_LEFT || direction == FOCUS_UP || direction == FOCUS_BACKWARD) {
        // Trying to move left and nothing there; try to page.
        handled = pageLeft();
    } else if (direction == FOCUS_RIGHT || direction == FOCUS_DOWN || direction == FOCUS_FORWARD) {
        // Trying to move right and nothing there; try to page.
        handled = pageRight();
    }
    if (handled) {
        playSoundEffect(SoundEffectConstants.getContantForFocusDirection(direction));
    }
    return handled;
}

From source file:com.dishes.views.stageredggridview.StaggeredGridView.java

/**
 * Should be called with mPopulating set to true
 * /*from   ww w .j  av  a2 s.c o  m*/
 * @param fromPosition
 *            Position to start filling from
 * @param overhang
 *            the number of extra pixels to fill beyond the current bottom
 *            edge
 * @return the max overhang beyond the end of the view of any added items at
 *         the bottom
 */
final int fillDown(int fromPosition, int overhang) {

    final int paddingLeft = getPaddingLeft();
    final int paddingRight = getPaddingRight();
    // final int itemMargin = mItemMargin;
    final int leftMargin = mItemLeftMargin;
    final int rightMargin = mItemRightMargin;
    final int topMargin = mItemTopMargin;
    final int bottomMargin = mItemBottomMargin;

    final int colWidth = (getWidth() - paddingLeft - paddingRight
            - (leftMargin + rightMargin) * (mColCount - 1)) / mColCount;
    final int gridBottom = getHeight() - getPaddingBottom();
    final int fillTo = gridBottom + overhang;
    int nextCol = getNextColumnDown(fromPosition);
    int position = fromPosition;

    while (nextCol >= 0 && mItemBottoms[nextCol] < fillTo && position < mItemCount) {

        final View child = obtainView(position, null);

        if (child == null)
            continue;

        LayoutParams lp = (LayoutParams) child.getLayoutParams();
        if (lp == null) {
            lp = this.generateDefaultLayoutParams();
            child.setLayoutParams(lp);
        }
        if (child.getParent() != this) {
            if (mInLayout) {
                addViewInLayout(child, -1, lp);
            } else {
                addView(child);
            }
        }

        final int span = Math.min(mColCount, lp.span);
        final int widthSize = colWidth * span + (leftMargin + rightMargin) * (span - 1);
        final int widthSpec = MeasureSpec.makeMeasureSpec(widthSize, MeasureSpec.EXACTLY);

        LayoutRecord rec;
        if (span > 1) {
            rec = getNextRecordDown(position, span);
            // nextCol = rec.column;
        } else {
            rec = mLayoutRecords.get(position);
        }

        boolean invalidateAfter = false;
        if (rec == null) {
            rec = new LayoutRecord();
            mLayoutRecords.put(position, rec);
            rec.column = nextCol;
            rec.span = span;
        } else if (span != rec.span) {
            rec.span = span;
            rec.column = nextCol;
            invalidateAfter = true;
        } else {
            // nextCol = rec.column;
        }

        if (mHasStableIds) {
            final long id = mAdapter.getItemId(position);
            rec.id = id;
            lp.id = id;
        }

        lp.column = nextCol;

        final int heightSpec;
        if (lp.height == LayoutParams.WRAP_CONTENT) {
            heightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
        } else {
            heightSpec = MeasureSpec.makeMeasureSpec(lp.height, MeasureSpec.EXACTLY);
        }
        child.measure(widthSpec, heightSpec);

        final int childHeight = child.getMeasuredHeight();
        if (invalidateAfter || (childHeight != rec.height && rec.height > 0)) {
            invalidateLayoutRecordsAfterPosition(position);
        }
        rec.height = childHeight;

        final int startFrom;
        if (span > 1) {
            int lowest = mItemBottoms[nextCol];
            for (int i = nextCol + 1; i < nextCol + span; i++) {
                final int bottom = mItemBottoms[i];
                if (bottom > lowest) {
                    lowest = bottom;
                }
            }
            startFrom = lowest;
        } else {
            startFrom = mItemBottoms[nextCol];
        }

        final int childTop = startFrom + topMargin;
        final int childBottom = childTop + childHeight - bottomMargin;
        final int childLeft = paddingLeft + nextCol * (colWidth + leftMargin);
        final int childRight = childLeft + child.getMeasuredWidth() - rightMargin;
        child.layout(childLeft, childTop, childRight, childBottom);

        // add the position to the mapping
        if (!mColMappings.get(nextCol).contains(position)) {

            // check to see if the mapping exists in other columns
            // this would happen if list has been updated
            for (ArrayList<Integer> list : mColMappings) {
                if (list.contains(position)) {
                    list.remove((Integer) position);
                }
            }

            mColMappings.get(nextCol).add(position);

        }

        for (int i = nextCol; i < nextCol + span; i++) {
            mItemBottoms[i] = childBottom + rec.getMarginBelow(i - nextCol);
        }

        position++;
        nextCol = getNextColumnDown(position);
    }

    int lowestView = 0;
    for (int i = 0; i < mColCount; i++) {
        if (mItemBottoms[i] > lowestView) {
            lowestView = mItemBottoms[i];
        }
    }
    return lowestView - gridBottom;
}

From source file:com.ad.view.staggeredgridview.StaggeredGridView.java

/**
 * Obtain a populated view from the adapter. If optScrap is non-null and is
 * not reused it will be placed in the recycle bin.
 * /*  w  w w .ja v a  2s  .  com*/
 * @param position
 *            position to get view for
 * @param optScrap
 *            Optional scrap view; will be reused if possible
 * @return A new view, a recycled view from mRecycler, or optScrap
 */
final View obtainView(int position, View optScrap) {
    View view = mRecycler.getTransientStateView(position);
    if (view != null) {
        return view;
    }

    if (position >= mAdapter.getCount()) {
        view = null;
        return null;
    }

    // Reuse optScrap if it's of the right type (and not null)
    final int optType = optScrap != null ? ((LayoutParams) optScrap.getLayoutParams()).viewType : -1;
    final int positionViewType = mAdapter.getItemViewType(position);
    final View scrap = optType == positionViewType ? optScrap : mRecycler.getScrapView(positionViewType);

    view = mAdapter.getView(position, scrap, this);

    if (view != scrap && scrap != null) {
        // The adapter didn't use it; put it back.
        mRecycler.addScrap(scrap);
    }

    ViewGroup.LayoutParams lp = view.getLayoutParams();

    if (view.getParent() != this) {
        if (lp == null) {
            lp = generateDefaultLayoutParams();
        } else if (!checkLayoutParams(lp)) {
            lp = generateLayoutParams(lp);
        }
    }

    final LayoutParams sglp = (LayoutParams) lp;
    sglp.position = position;
    sglp.viewType = positionViewType;

    // Set the updated LayoutParam before returning the view.
    view.setLayoutParams(sglp);
    return view;
}

From source file:com.dishes.views.stageredggridview.StaggeredGridView.java

/**
 * Should be called with mPopulating set to true
 * //from w  ww  .ja va 2 s .  c  o m
 * @param fromPosition
 *            Position to start filling from
 * @param overhang
 *            the number of extra pixels to fill beyond the current top edge
 * @return the max overhang beyond the beginning of the view of any added
 *         items at the top
 */
final int fillUp(int fromPosition, int overhang) {

    final int paddingLeft = getPaddingLeft();
    final int paddingRight = getPaddingRight();
    // final int itemMargin = mItemMargin;
    final int leftMargin = mItemLeftMargin;
    final int rightMargin = mItemRightMargin;
    final int topMargin = mItemTopMargin;
    final int bottomMargin = mItemBottomMargin;

    final int colWidth = (getWidth() - paddingLeft - paddingRight
            - (leftMargin + rightMargin) * (mColCount - 1)) / mColCount;
    mColWidth = colWidth;
    final int gridTop = getPaddingTop();
    final int fillTo = gridTop - overhang;
    int nextCol = getNextColumnUp();
    int position = fromPosition;

    while (nextCol >= 0 && mItemTops[nextCol] > fillTo && position >= 0) {
        // make sure the nextCol is correct. check to see if has been mapped
        // otherwise stick to getNextColumnUp()
        if (!mColMappings.get(nextCol).contains((Integer) position)) {
            for (int i = 0; i < mColMappings.size(); i++) {
                if (mColMappings.get(i).contains((Integer) position)) {
                    nextCol = i;
                    break;
                }
            }
        }

        // displayMapping();

        final View child = obtainView(position, null);

        if (child == null)
            continue;

        LayoutParams lp = (LayoutParams) child.getLayoutParams();

        if (lp == null) {
            lp = this.generateDefaultLayoutParams();
            child.setLayoutParams(lp);
        }

        if (child.getParent() != this) {
            if (mInLayout) {
                addViewInLayout(child, 0, lp);
            } else {
                addView(child, 0);
            }
        }

        final int span = Math.min(mColCount, lp.span);
        final int widthSize = colWidth * span + (leftMargin + rightMargin) * (span - 1);
        final int widthSpec = MeasureSpec.makeMeasureSpec(widthSize, MeasureSpec.EXACTLY);

        LayoutRecord rec;
        if (span > 1) {
            rec = getNextRecordUp(position, span);
            // nextCol = rec.column;
        } else {
            rec = mLayoutRecords.get(position);
        }

        boolean invalidateBefore = false;
        if (rec == null) {
            rec = new LayoutRecord();
            mLayoutRecords.put(position, rec);
            rec.column = nextCol;
            rec.span = span;
        } else if (span != rec.span) {
            rec.span = span;
            rec.column = nextCol;
            invalidateBefore = true;
        } else {
            // nextCol = rec.column;
        }

        if (mHasStableIds) {
            final long id = mAdapter.getItemId(position);
            rec.id = id;
            lp.id = id;
        }

        lp.column = nextCol;

        final int heightSpec;
        if (lp.height == LayoutParams.WRAP_CONTENT) {
            heightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
        } else {
            heightSpec = MeasureSpec.makeMeasureSpec(lp.height, MeasureSpec.EXACTLY);
        }
        child.measure(widthSpec, heightSpec);

        final int childHeight = child.getMeasuredHeight();
        if (invalidateBefore || (childHeight != rec.height && rec.height > 0)) {
            invalidateLayoutRecordsBeforePosition(position);
        }
        rec.height = childHeight;

        // int itemTop = mItemTops[nextCol];

        final int startFrom;
        if (span > 1) {
            int highest = mItemTops[nextCol];
            for (int i = nextCol + 1; i < nextCol + span; i++) {
                final int top = mItemTops[i];
                if (top < highest) {
                    highest = top;
                }
            }
            startFrom = highest;
        } else {
            startFrom = mItemTops[nextCol];
        }

        int childBottom = startFrom;
        int childTop = childBottom - childHeight;
        final int childLeft = paddingLeft + nextCol * (colWidth + (leftMargin + rightMargin));
        final int childRight = childLeft + child.getMeasuredWidth();

        // if(position == 0){
        // if(this.getChildCount()>1 && this.mColCount>1){
        // childTop = this.getChildAt(1).getTop();
        // childBottom = childTop + childHeight;
        // }
        // }

        child.layout(childLeft, childTop, childRight, childBottom);

        for (int i = nextCol; i < nextCol + span; i++) {
            mItemTops[i] = childTop - rec.getMarginAbove(i - nextCol) - (topMargin + bottomMargin);
        }

        nextCol = getNextColumnUp();
        mFirstPosition = position--;
    }

    int highestView = getHeight();

    for (int i = 0; i < mColCount; i++) {
        final View child = getFirstChildAtColumn(i);
        if (child == null) {
            highestView = 0;
            break;
        }
        final int top = child.getTop();

        if (top < highestView) {
            highestView = top;
        }
    }

    return gridTop - highestView;
}

From source file:com.viewpagerindicator.MyDirectionalViewPager.java

public boolean arrowScroll(int direction) {
    View currentFocused = findFocus();
    if (currentFocused == this) {
        currentFocused = null;//from w  ww.jav a  2  s.c  o m
    } else if (currentFocused != null) {
        boolean isChild = false;
        for (ViewParent parent = currentFocused.getParent(); parent instanceof ViewGroup; parent = parent
                .getParent()) {
            if (parent == this) {
                isChild = true;
                break;
            }
        }
        if (!isChild) {
            // This would cause the focus search down below to fail in fun ways.
            final StringBuilder sb = new StringBuilder();
            sb.append(currentFocused.getClass().getSimpleName());
            for (ViewParent parent = currentFocused.getParent(); parent instanceof ViewGroup; parent = parent
                    .getParent()) {
                sb.append(" => ").append(parent.getClass().getSimpleName());
            }
            Log.e(TAG, "arrowScroll tried to find focus based on non-child " + "current focused view "
                    + sb.toString());
            currentFocused = null;
        }
    }

    boolean handled = false;

    View nextFocused = FocusFinder.getInstance().findNextFocus(this, currentFocused, direction);
    if (nextFocused != null && nextFocused != currentFocused) {
        if (direction == View.FOCUS_LEFT) {
            // If there is nothing to the left, or this is causing us to
            // jump to the right, then what we really want to do is page left.
            final int nextLeft = getChildRectInPagerCoordinates(mTempRect, nextFocused).left;
            final int currLeft = getChildRectInPagerCoordinates(mTempRect, currentFocused).left;
            if (currentFocused != null && nextLeft >= currLeft) {
                handled = pagePre();
            } else {
                handled = nextFocused.requestFocus();
            }
        } else if (direction == View.FOCUS_RIGHT) {
            // If there is nothing to the right, or this is causing us to
            // jump to the left, then what we really want to do is page right.
            final int nextLeft = getChildRectInPagerCoordinates(mTempRect, nextFocused).left;
            final int currLeft = getChildRectInPagerCoordinates(mTempRect, currentFocused).left;
            if (currentFocused != null && nextLeft <= currLeft) {
                handled = pageNext();
            } else {
                handled = nextFocused.requestFocus();
            }
        }
    } else if (direction == FOCUS_LEFT || direction == FOCUS_BACKWARD) {
        // Trying to move left and nothing there; try to page.
        handled = pagePre();
    } else if (direction == FOCUS_RIGHT || direction == FOCUS_FORWARD) {
        // Trying to move right and nothing there; try to page.
        handled = pageNext();
    }
    if (handled) {
        playSoundEffect(SoundEffectConstants.getContantForFocusDirection(direction));
    }
    return handled;
}

From source file:com.app.afteryou.ui.staggered.StaggeredGridView.java

/**
 * Obtain a populated view from the adapter. If optScrap is non-null and is
 * not reused it will be placed in the recycle bin.
 * //from w  ww.j  a  v a  2 s . c o  m
 * @param position
 *            position to get view for
 * @param optScrap
 *            Optional scrap view; will be reused if possible
 * @return A new view, a recycled view from mRecycler, or optScrap
 */
final View obtainView(int position, View optScrap) {
    View view = mRecycler.getTransientStateView(position);
    if (view != null) {
        return view;
    }

    if (position >= mAdapter.getCount()) {
        view = null;
        return null;
    }

    // Reuse optScrap if it's of the right type (and not null)
    final int optType = optScrap != null ? ((LayoutParams) optScrap.getLayoutParams()).viewType : -1;
    final int positionViewType = mAdapter.getItemViewType(position);
    final View scrap = optType == positionViewType ? optScrap : mRecycler.getScrapView(positionViewType);

    view = mAdapter.getView(position, scrap, this);

    if (view != scrap && scrap != null) {
        // The adapter didn't use it; put it back.
        mRecycler.addScrap(scrap);
    }

    ViewGroup.LayoutParams lp = view.getLayoutParams();

    if (view.getParent() != this) {
        if (lp == null) {
            lp = generateDefaultLayoutParams();
        } else if (!checkLayoutParams(lp)) {
            lp = generateLayoutParams(lp);
        }
    }

    final LayoutParams sglp = (LayoutParams) lp;
    sglp.position = position;
    sglp.viewType = positionViewType;
    view.setLayoutParams(sglp);

    return view;
}

From source file:chan.android.app.bitwise.util.StaggeredGridView.java

/**
 * Obtain a populated view from the adapter. If optScrap is non-null and is not
 * reused it will be placed in the recycle bin.
 *
 * @param position position to get view for
 * @param optScrap Optional scrap view; will be reused if possible
 * @return A new view, a recycled view from mRecycler, or optScrap
 *//*from   w w w .j  ava2 s. co m*/
final View obtainView(int position, View optScrap) {
    View view = mRecycler.getTransientStateView(position);
    if (view != null) {
        return view;
    }

    if (position >= mAdapter.getCount()) {
        view = null;
        return null;
    }

    // Reuse optScrap if it's of the right type (and not null)
    final int optType = optScrap != null ? ((LayoutParams) optScrap.getLayoutParams()).viewType : -1;
    final int positionViewType = mAdapter.getItemViewType(position);
    final View scrap = optType == positionViewType ? optScrap : mRecycler.getScrapView(positionViewType);

    view = mAdapter.getView(position, scrap, this);

    if (view != scrap && scrap != null) {
        // The adapter didn't use it; put it back.
        mRecycler.addScrap(scrap);
    }

    ViewGroup.LayoutParams lp = view.getLayoutParams();

    if (view.getParent() != this) {
        if (lp == null) {
            lp = generateDefaultLayoutParams();
        } else if (!checkLayoutParams(lp)) {
            lp = generateLayoutParams(lp);
        }
    }

    final LayoutParams sglp = (LayoutParams) lp;
    sglp.position = position;
    sglp.viewType = positionViewType;

    return view;
}