Example usage for android.view View getLeft

List of usage examples for android.view View getLeft

Introduction

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

Prototype

@ViewDebug.CapturedViewProperty
public final int getLeft() 

Source Link

Document

Left position of this view relative to its parent.

Usage

From source file:com.chauthai.swipereveallayout.ViewDragHelper.java

/**
 * Check if this event as provided to the parent view's onInterceptTouchEvent should
 * cause the parent to intercept the touch event stream.
 *
 * @param ev MotionEvent provided to onInterceptTouchEvent
 * @return true if the parent view should return true from onInterceptTouchEvent
 *///from  w  w  w  .ja v a  2s  . c  om
public boolean shouldInterceptTouchEvent(MotionEvent ev) {
    final int action = MotionEventCompat.getActionMasked(ev);
    final int actionIndex = MotionEventCompat.getActionIndex(ev);
    if (action == MotionEvent.ACTION_DOWN) {
        // Reset things for a new event stream, just in case we didn't get
        // the whole previous stream.
        cancel();
    }
    if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    }
    mVelocityTracker.addMovement(ev);
    switch (action) {
    case MotionEvent.ACTION_DOWN: {
        final float x = ev.getX();
        final float y = ev.getY();
        final int pointerId = MotionEventCompat.getPointerId(ev, 0);
        saveInitialMotion(x, y, pointerId);
        final View toCapture = findTopChildUnder((int) x, (int) y);
        // Catch a settling view if possible.
        if (toCapture == mCapturedView && mDragState == STATE_SETTLING) {
            tryCaptureViewForDrag(toCapture, pointerId);
        }
        final int edgesTouched = mInitialEdgesTouched[pointerId];
        if ((edgesTouched & mTrackingEdges) != 0) {
            mCallback.onEdgeTouched(edgesTouched & mTrackingEdges, pointerId);
        }
        break;
    }
    case MotionEventCompat.ACTION_POINTER_DOWN: {
        final int pointerId = MotionEventCompat.getPointerId(ev, actionIndex);
        final float x = MotionEventCompat.getX(ev, actionIndex);
        final float y = MotionEventCompat.getY(ev, actionIndex);
        saveInitialMotion(x, y, pointerId);
        // A ViewDragHelper can only manipulate one view at a time.
        if (mDragState == STATE_IDLE) {
            final int edgesTouched = mInitialEdgesTouched[pointerId];
            if ((edgesTouched & mTrackingEdges) != 0) {
                mCallback.onEdgeTouched(edgesTouched & mTrackingEdges, pointerId);
            }
        } else if (mDragState == STATE_SETTLING) {
            // Catch a settling view if possible.
            final View toCapture = findTopChildUnder((int) x, (int) y);
            if (toCapture == mCapturedView) {
                tryCaptureViewForDrag(toCapture, pointerId);
            }
        }
        break;
    }
    case MotionEvent.ACTION_MOVE: {
        if (mInitialMotionX == null || mInitialMotionY == null)
            break;
        // First to cross a touch slop over a draggable view wins. Also report edge drags.
        final int pointerCount = MotionEventCompat.getPointerCount(ev);
        for (int i = 0; i < pointerCount; i++) {
            final int pointerId = MotionEventCompat.getPointerId(ev, i);
            // If pointer is invalid then skip the ACTION_MOVE.
            if (!isValidPointerForActionMove(pointerId))
                continue;
            final float x = MotionEventCompat.getX(ev, i);
            final float y = MotionEventCompat.getY(ev, i);
            final float dx = x - mInitialMotionX[pointerId];
            final float dy = y - mInitialMotionY[pointerId];
            final View toCapture = findTopChildUnder((int) x, (int) y);
            final boolean pastSlop = toCapture != null && checkTouchSlop(toCapture, dx, dy);
            if (pastSlop) {
                // check the callback's
                // getView[Horizontal|Vertical]DragRange methods to know
                // if you can move at all along an axis, then see if it
                // would clamp to the same value. If you can't move at
                // all in every dimension with a nonzero range, bail.
                final int oldLeft = toCapture.getLeft();
                final int targetLeft = oldLeft + (int) dx;
                final int newLeft = mCallback.clampViewPositionHorizontal(toCapture, targetLeft, (int) dx);
                final int oldTop = toCapture.getTop();
                final int targetTop = oldTop + (int) dy;
                final int newTop = mCallback.clampViewPositionVertical(toCapture, targetTop, (int) dy);
                final int horizontalDragRange = mCallback.getViewHorizontalDragRange(toCapture);
                final int verticalDragRange = mCallback.getViewVerticalDragRange(toCapture);
                if ((horizontalDragRange == 0 || horizontalDragRange > 0 && newLeft == oldLeft)
                        && (verticalDragRange == 0 || verticalDragRange > 0 && newTop == oldTop)) {
                    break;
                }
            }
            reportNewEdgeDrags(dx, dy, pointerId);
            if (mDragState == STATE_DRAGGING) {
                // Callback might have started an edge drag
                break;
            }
            if (pastSlop && tryCaptureViewForDrag(toCapture, pointerId)) {
                break;
            }
        }
        saveLastMotion(ev);
        break;
    }
    case MotionEventCompat.ACTION_POINTER_UP: {
        final int pointerId = MotionEventCompat.getPointerId(ev, actionIndex);
        clearMotionHistory(pointerId);
        break;
    }
    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_CANCEL: {
        cancel();
        break;
    }
    }
    return mDragState == STATE_DRAGGING;
}

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

/**
 * This method will be invoked when the current page is scrolled, either as part
 * of a programmatically initiated smooth scroll or a user initiated touch scroll.
 * If you override this method you must call through to the superclass implementation
 * (e.g. super.onPageScrolled(position, offset, offsetPixels)) before onPageScrolled
 * returns./*from  w w  w. j a v a  2  s  . c  om*/
 *
 * @param position Position index of the first page currently being displayed.
 *                 Page position+1 will be visible if positionOffset is nonzero.
 * @param offset Value from [0, 1) indicating the offset from the page at position.
 * @param offsetPixels Value in pixels indicating the offset from position.
 */
protected void onPageScrolled(int position, float offset, int offsetPixels) {
    // Offset any decor views if needed - keep them on-screen at all times.
    if (mDecorChildCount > 0) {
        // TODO This is where I start getting tired. Refactor this better later.
        if (isOrientationHorizontal()) {
            final int scrollX = getScrollX();
            int paddingLeft = getPaddingLeft();
            int paddingRight = getPaddingRight();
            final int width = getWidth();
            final int childCount = getChildCount();
            for (int i = 0; i < childCount; i++) {
                final View child = getChildAt(i);
                final LayoutParams lp = (LayoutParams) child.getLayoutParams();
                if (!lp.isDecor)
                    continue;

                final int hgrav = lp.gravity & Gravity.HORIZONTAL_GRAVITY_MASK;
                int childLeft = 0;
                switch (hgrav) {
                default:
                    childLeft = paddingLeft;
                    break;
                case Gravity.LEFT:
                    childLeft = paddingLeft;
                    paddingLeft += child.getWidth();
                    break;
                case Gravity.CENTER_HORIZONTAL:
                    childLeft = Math.max((width - child.getMeasuredWidth()) / 2, paddingLeft);
                    break;
                case Gravity.RIGHT:
                    childLeft = width - paddingRight - child.getMeasuredWidth();
                    paddingRight += child.getMeasuredWidth();
                    break;
                }
                childLeft += scrollX;

                final int childOffset = childLeft - child.getLeft();
                if (childOffset != 0) {
                    child.offsetLeftAndRight(childOffset);
                }
            }
        } else {
            final int scrollY = getScrollY();
            int paddingTop = getPaddingTop();
            int paddingBottom = getPaddingBottom();
            final int height = getHeight();
            final int childCount = getChildCount();
            for (int i = 0; i < childCount; i++) {
                final View child = getChildAt(i);
                final LayoutParams lp = (LayoutParams) child.getLayoutParams();
                if (!lp.isDecor)
                    continue;

                final int vgrav = lp.gravity & Gravity.VERTICAL_GRAVITY_MASK;
                int childTop = 0;
                switch (vgrav) {
                default:
                    childTop = paddingTop;
                    break;
                case Gravity.TOP:
                    childTop = paddingTop;
                    paddingTop += child.getHeight();
                    break;
                case Gravity.CENTER_VERTICAL:
                    childTop = Math.max((height - child.getMeasuredHeight()) / 2, paddingTop);
                    break;
                case Gravity.BOTTOM:
                    childTop = height - paddingBottom - child.getMeasuredHeight();
                    paddingBottom += child.getMeasuredHeight();
                    break;
                }
                childTop += scrollY;

                final int childOffset = childTop - child.getTop();
                if (childOffset != 0) {
                    child.offsetTopAndBottom(childOffset);
                }
            }
        }
    }

    if (mOnPageChangeListener != null) {
        mOnPageChangeListener.onPageScrolled(position, offset, offsetPixels);
    }
    if (mInternalPageChangeListener != null) {
        mInternalPageChangeListener.onPageScrolled(position, offset, offsetPixels);
    }

    if (mPageTransformer != null) {
        final boolean horizontal = isOrientationHorizontal();
        final int scroll = horizontal ? getScrollX() : getScrollY();
        final int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            final View child = getChildAt(i);
            final LayoutParams lp = (LayoutParams) child.getLayoutParams();

            if (lp.isDecor)
                continue;

            float transformPos;
            if (horizontal) {
                transformPos = (float) (child.getLeft() - scroll) / getClientWidth();
            } else {
                transformPos = (float) (child.getTop() - scroll) / getClientHeight();
            }
            mPageTransformer.transformPage(child, transformPos);
        }
    }

    mCalledSuper = true;
}

From source file:com.awrtechnologies.valor.valorfireplace.hlistview.widget.HListView.java

/**
 * Fills the list based on positioning the new selection relative to the old selection. The new selection will be placed at,
 * above, or below the location of the new selection depending on how the selection is moving. The selection will then be pinned
 * to the visible part of the screen, excluding the edges that are faded. The list is then filled upwards and downwards from
 * there./* www. j  a va 2 s . co  m*/
 *
 * @param oldSel
 *           The old selected view. Useful for trying to put the new selection in the same place
 * @param newSel
 *           The view that is to become selected. Useful for trying to put the new selection in the same place
 * @param delta
 *           Which way we are moving
 * @param childrenLeft
 *           Where to start drawing children
 * @param childrenRight
 *           Last pixel where children can be drawn
 * @return The view that currently has selection
 */
private View moveSelection(View oldSel, View newSel, int delta, int childrenLeft, int childrenRight) {
    int fadingEdgeLength = getHorizontalFadingEdgeLength();
    final int selectedPosition = mSelectedPosition;

    View sel;

    final int leftSelectionPixel = getLeftSelectionPixel(childrenLeft, fadingEdgeLength, selectedPosition);
    final int rightSelectionPixel = getRightSelectionPixel(childrenLeft, fadingEdgeLength, selectedPosition);

    if (delta > 0) {
        /*
         * Case 1: Scrolling down.
         */

        /*
         * Before After | | | | +-------+ +-------+ | A | | A | | 1 | => +-------+ +-------+ | B | | B | | 2 | +-------+ +-------+
         * | | | |
         *
         * Try to keep the top of the previously selected item where it was. oldSel = A sel = B
         */

        // Put oldSel (A) where it belongs
        oldSel = makeAndAddView(selectedPosition - 1, oldSel.getLeft(), true, mListPadding.top, false);

        final int dividerWidth = mDividerWidth;

        // Now put the new selection (B) below that
        sel = makeAndAddView(selectedPosition, oldSel.getRight() + dividerWidth, true, mListPadding.top, true);

        // Some of the newly selected item extends below the bottom of the list
        if (sel.getRight() > rightSelectionPixel) {

            // Find space available above the selection into which we can scroll upwards
            int spaceBefore = sel.getLeft() - leftSelectionPixel;

            // Find space required to bring the bottom of the selected item fully into view
            int spaceAfter = sel.getRight() - rightSelectionPixel;

            // Don't scroll more than half the height of the list
            int halfHorizontalSpace = (childrenRight - childrenLeft) / 2;
            int offset = Math.min(spaceBefore, spaceAfter);
            offset = Math.min(offset, halfHorizontalSpace);

            // We placed oldSel, so offset that item
            oldSel.offsetLeftAndRight(-offset);
            // Now offset the selected item to get it into view
            sel.offsetLeftAndRight(-offset);
        }

        // Fill in views above and below
        if (!mStackFromRight) {
            fillLeft(mSelectedPosition - 2, sel.getLeft() - dividerWidth);
            adjustViewsLeftOrRight();
            fillRight(mSelectedPosition + 1, sel.getRight() + dividerWidth);
        } else {
            fillRight(mSelectedPosition + 1, sel.getRight() + dividerWidth);
            adjustViewsLeftOrRight();
            fillLeft(mSelectedPosition - 2, sel.getLeft() - dividerWidth);
        }
    } else if (delta < 0) {
        /*
         * Case 2: Scrolling up.
         */

        /*
         * Before After | | | | +-------+ +-------+ | A | | A | +-------+ => | 1 | | B | +-------+ | 2 | | B | +-------+ +-------+
         * | | | |
         *
         * Try to keep the top of the item about to become selected where it was. newSel = A olSel = B
         */

        if (newSel != null) {
            // Try to position the top of newSel (A) where it was before it was selected
            sel = makeAndAddView(selectedPosition, newSel.getLeft(), true, mListPadding.top, true);
        } else {
            // If (A) was not on screen and so did not have a view, position
            // it above the oldSel (B)
            sel = makeAndAddView(selectedPosition, oldSel.getLeft(), false, mListPadding.top, true);
        }

        // Some of the newly selected item extends above the top of the list
        if (sel.getLeft() < leftSelectionPixel) {
            // Find space required to bring the top of the selected item fully into view
            int spaceBefore = leftSelectionPixel - sel.getLeft();

            // Find space available below the selection into which we can scroll downwards
            int spaceAfter = rightSelectionPixel - sel.getRight();

            // Don't scroll more than half the height of the list
            int halfHorizontalSpace = (childrenRight - childrenLeft) / 2;
            int offset = Math.min(spaceBefore, spaceAfter);
            offset = Math.min(offset, halfHorizontalSpace);

            // Offset the selected item to get it into view
            sel.offsetLeftAndRight(offset);
        }

        // Fill in views above and below
        fillBeforeAndAfter(sel, selectedPosition);
    } else {

        int oldLeft = oldSel.getLeft();

        /*
         * Case 3: Staying still
         */
        sel = makeAndAddView(selectedPosition, oldLeft, true, mListPadding.top, true);

        // We're staying still...
        if (oldLeft < childrenLeft) {
            // ... but the top of the old selection was off screen.
            // (This can happen if the data changes size out from under us)
            int newRight = sel.getRight();
            if (newRight < childrenLeft + 20) {
                // Not enough visible -- bring it onscreen
                sel.offsetLeftAndRight(childrenLeft - sel.getLeft());
            }
        }

        // Fill in views above and below
        fillBeforeAndAfter(sel, selectedPosition);
    }

    return sel;
}

From source file:com.awrtechnologies.carbudgetsales.hlistview.widget.HListView.java

/**
 * Fills the list based on positioning the new selection relative to the old selection. The new selection will be placed at,
 * above, or below the location of the new selection depending on how the selection is moving. The selection will then be pinned
 * to the visible part of the screen, excluding the edges that are faded. The list is then filled upwards and downwards from
 * there.// ww w .  j  a  v a 2  s. co  m
 * 
 * @param oldSel
 *           The old selected view. Useful for trying to put the new selection in the same place
 * @param newSel
 *           The view that is to become selected. Useful for trying to put the new selection in the same place
 * @param delta
 *           Which way we are moving
 * @param childrenLeft
 *           Where to start drawing children
 * @param childrenRight
 *           Last pixel where children can be drawn
 * @return The view that currently has selection
 */
private View moveSelection(View oldSel, View newSel, int delta, int childrenLeft, int childrenRight) {
    int fadingEdgeLength = getHorizontalFadingEdgeLength();
    final int selectedPosition = mSelectedPosition;

    View sel;

    final int leftSelectionPixel = getLeftSelectionPixel(childrenLeft, fadingEdgeLength, selectedPosition);
    final int rightSelectionPixel = getRightSelectionPixel(childrenLeft, fadingEdgeLength, selectedPosition);

    if (delta > 0) {
        /*
         * Case 1: Scrolling down.
         */

        /*
         * Before After | | | | +-------+ +-------+ | A | | A | | 1 | => +-------+ +-------+ | B | | B | | 2 | +-------+ +-------+
         * | | | |
         * 
         * Try to keep the top of the previously selected item where it was. oldSel = A sel = B
         */

        // Put oldSel (A) where it belongs
        oldSel = makeAndAddView(selectedPosition - 1, oldSel.getLeft(), true, mListPadding.top, false);

        final int dividerWidth = mDividerWidth;

        // Now put the new selection (B) below that
        sel = makeAndAddView(selectedPosition, oldSel.getRight() + dividerWidth, true, mListPadding.top, true);

        // Some of the newly selected item extends below the bottom of the list
        if (sel.getRight() > rightSelectionPixel) {

            // Find space available above the selection into which we can scroll upwards
            int spaceBefore = sel.getLeft() - leftSelectionPixel;

            // Find space required to bring the bottom of the selected item fully into view
            int spaceAfter = sel.getRight() - rightSelectionPixel;

            // Don't scroll more than half the height of the list
            int halfHorizontalSpace = (childrenRight - childrenLeft) / 2;
            int offset = Math.min(spaceBefore, spaceAfter);
            offset = Math.min(offset, halfHorizontalSpace);

            // We placed oldSel, so offset that item
            oldSel.offsetLeftAndRight(-offset);
            // Now offset the selected item to get it into view
            sel.offsetLeftAndRight(-offset);
        }

        // Fill in views above and below
        if (!mStackFromRight) {
            fillLeft(mSelectedPosition - 2, sel.getLeft() - dividerWidth);
            adjustViewsLeftOrRight();
            fillRight(mSelectedPosition + 1, sel.getRight() + dividerWidth);
        } else {
            fillRight(mSelectedPosition + 1, sel.getRight() + dividerWidth);
            adjustViewsLeftOrRight();
            fillLeft(mSelectedPosition - 2, sel.getLeft() - dividerWidth);
        }
    } else if (delta < 0) {
        /*
         * Case 2: Scrolling up.
         */

        /*
         * Before After | | | | +-------+ +-------+ | A | | A | +-------+ => | 1 | | B | +-------+ | 2 | | B | +-------+ +-------+
         * | | | |
         * 
         * Try to keep the top of the item about to become selected where it was. newSel = A olSel = B
         */

        if (newSel != null) {
            // Try to position the top of newSel (A) where it was before it was selected
            sel = makeAndAddView(selectedPosition, newSel.getLeft(), true, mListPadding.top, true);
        } else {
            // If (A) was not on screen and so did not have a view, position
            // it above the oldSel (B)
            sel = makeAndAddView(selectedPosition, oldSel.getLeft(), false, mListPadding.top, true);
        }

        // Some of the newly selected item extends above the top of the list
        if (sel.getLeft() < leftSelectionPixel) {
            // Find space required to bring the top of the selected item fully into view
            int spaceBefore = leftSelectionPixel - sel.getLeft();

            // Find space available below the selection into which we can scroll downwards
            int spaceAfter = rightSelectionPixel - sel.getRight();

            // Don't scroll more than half the height of the list
            int halfHorizontalSpace = (childrenRight - childrenLeft) / 2;
            int offset = Math.min(spaceBefore, spaceAfter);
            offset = Math.min(offset, halfHorizontalSpace);

            // Offset the selected item to get it into view
            sel.offsetLeftAndRight(offset);
        }

        // Fill in views above and below
        fillBeforeAndAfter(sel, selectedPosition);
    } else {

        int oldLeft = oldSel.getLeft();

        /*
         * Case 3: Staying still
         */
        sel = makeAndAddView(selectedPosition, oldLeft, true, mListPadding.top, true);

        // We're staying still...
        if (oldLeft < childrenLeft) {
            // ... but the top of the old selection was off screen.
            // (This can happen if the com.awrtechnologies.carbudgetsales.data changes size out from under us)
            int newRight = sel.getRight();
            if (newRight < childrenLeft + 20) {
                // Not enough visible -- bring it onscreen
                sel.offsetLeftAndRight(childrenLeft - sel.getLeft());
            }
        }

        // Fill in views above and below
        fillBeforeAndAfter(sel, selectedPosition);
    }

    return sel;
}

From source file:app.umitems.greenclock.widget.sgv.StaggeredGridView.java

/**
 * Initiate the dragging process. Create a bitmap that is displayed as the dragging event
 * happens and is moved around across the screen.  This function is called once for each time
 * that a dragging event is initiated./*w  w w .j  ava 2 s. c  om*/
 *
 * The logic to this method was borrowed from the TouchInterceptor.java class from the
 * music app.
 *
 * @param draggedChild The child view being dragged
 * @param x The x coordinate of this view where dragging began
 * @param y The y coordinate of this view where dragging began
 */
private void startDragging(final View draggedChild, final int x, final int y) {
    if (!isDragReorderingSupported()) {
        return;
    }

    mDragBitmap = createDraggedChildBitmap(draggedChild);
    if (mDragBitmap == null) {
        // It appears that creating bitmaps for large views fail. For now, don't allow
        // dragging in this scenario.  When using the framework's drag and drop implementation,
        // drag shadow also fails with a OutofResourceException when trying to draw the drag
        // shadow onto a Surface.
        mReorderHelper.handleDragCancelled(draggedChild);
        return;
    }
    mTouchOffsetToChildLeft = x - draggedChild.getLeft();
    mTouchOffsetToChildTop = y - draggedChild.getTop();
    updateReorderStates(ReorderUtils.DRAG_STATE_DRAGGING);

    initializeDragScrollParameters(y);

    final LayoutParams params = (LayoutParams) draggedChild.getLayoutParams();
    mReorderHelper.handleDragStart(draggedChild, params.position, params.id,
            new Point(mTouchDownForDragStartX, mTouchDownForDragStartY));

    // TODO: Reconsider using the framework's DragShadow support for dragging,
    // and only draw the bitmap in onDrop for animation.
    final Context context = getContext();
    mDragView = new ImageView(context);
    mDragView.setImageBitmap(mDragBitmap);
    mDragView.setAlpha(160);

    mWindowParams = new WindowManager.LayoutParams();
    mWindowParams.gravity = Gravity.TOP | Gravity.START;

    mWindowParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
    mWindowParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
    mWindowParams.flags = mWindowManagerLayoutFlags;
    mWindowParams.format = PixelFormat.TRANSLUCENT;
    // Use WindowManager to overlay a transparent image on drag
    mWindowManager.addView(mDragView, mWindowParams);
    updateDraggedBitmapLocation(x, y);
}

From source file:com.appunite.list.HorizontalListView.java

/**
 * Fills the list based on positioning the new selection relative to the old
 * selection. The new selection will be placed at, above, or below the
 * location of the new selection depending on how the selection is moving.
 * The selection will then be pinned to the visible part of the screen,
 * excluding the edges that are faded. The list is then filled upwards and
 * downwards from there.//from ww  w .  ja v  a  2s  .c  o m
 *
 * @param oldSel The old selected view. Useful for trying to put the new
 *        selection in the same place
 * @param newSel The view that is to become selected. Useful for trying to
 *        put the new selection in the same place
 * @param delta Which way we are moving
 * @param childrenLeft Where to start drawing children
 * @param childrenRight Last pixel where children can be drawn
 * @return The view that currently has selection
 */
private View moveSelection(View oldSel, View newSel, int delta, int childrenLeft, int childrenRight) {
    int fadingEdgeLength = getHorizontalFadingEdgeLength();
    final int selectedPosition = mSelectedPosition;

    View sel;

    final int leftSelectionPixel = getLeftSelectionPixel(childrenLeft, fadingEdgeLength, selectedPosition);
    final int rightSelectionPixel = getRightSelectionPixel(childrenLeft, fadingEdgeLength, selectedPosition);

    if (delta > 0) {
        /*
         * Case 1: Scrolling down.
         */

        /*
         *     Before           After
         *    |       |        |       |
         *    +-------+        +-------+
         *    |   A   |        |   A   |
         *    |   1   |   =>   +-------+
         *    +-------+        |   B   |
         *    |   B   |        |   2   |
         *    +-------+        +-------+
         *    |       |        |       |
         *
         *    Try to keep the top of the previously selected item where it was.
         *    oldSel = A
         *    sel = B
         */

        // Put oldSel (A) where it belongs
        oldSel = makeAndAddView(selectedPosition - 1, oldSel.getLeft(), true, mListPadding.top, false);

        final int dividerWidth = mDividerWidth;

        // Now put the new selection (B) below that
        sel = makeAndAddView(selectedPosition, oldSel.getRight() + dividerWidth, true, mListPadding.top, true);

        // Some of the newly selected item extends below the bottom of the list
        if (sel.getRight() > rightSelectionPixel) {

            // Find space available above the selection into which we can scroll upwards
            int spaceAbove = sel.getLeft() - leftSelectionPixel;

            // Find space required to bring the bottom of the selected item fully into view
            int spaceBelow = sel.getRight() - rightSelectionPixel;

            // Don't scroll more than half the width of the list
            int halfHorizontalSpace = (childrenRight - childrenLeft) / 2;
            int offset = Math.min(spaceAbove, spaceBelow);
            offset = Math.min(offset, halfHorizontalSpace);

            // We placed oldSel, so offset that item
            oldSel.offsetLeftAndRight(-offset);
            // Now offset the selected item to get it into view
            sel.offsetLeftAndRight(-offset);
        }

        // Fill in views above and below
        if (!mStackFromRight) {
            fillLeft(mSelectedPosition - 2, sel.getLeft() - dividerWidth);
            adjustViewsLeftOrRight();
            fillRight(mSelectedPosition + 1, sel.getRight() + dividerWidth);
        } else {
            fillRight(mSelectedPosition + 1, sel.getRight() + dividerWidth);
            adjustViewsLeftOrRight();
            fillLeft(mSelectedPosition - 2, sel.getLeft() - dividerWidth);
        }
    } else if (delta < 0) {
        /*
         * Case 2: Scrolling up.
         */

        /*
         *     Before           After
         *    |       |        |       |
         *    +-------+        +-------+
         *    |   A   |        |   A   |
         *    +-------+   =>   |   1   |
         *    |   B   |        +-------+
         *    |   2   |        |   B   |
         *    +-------+        +-------+
         *    |       |        |       |
         *
         *    Try to keep the top of the item about to become selected where it was.
         *    newSel = A
         *    olSel = B
         */

        if (newSel != null) {
            // Try to position the top of newSel (A) where it was before it was selected
            sel = makeAndAddView(selectedPosition, newSel.getLeft(), true, mListPadding.top, true);
        } else {
            // If (A) was not on screen and so did not have a view, position
            // it above the oldSel (B)
            sel = makeAndAddView(selectedPosition, oldSel.getLeft(), false, mListPadding.top, true);
        }

        // Some of the newly selected item extends above the top of the list
        if (sel.getLeft() < leftSelectionPixel) {
            // Find space required to bring the top of the selected item fully into view
            int spaceAbove = leftSelectionPixel - sel.getLeft();

            // Find space available below the selection into which we can scroll downwards
            int spaceBelow = rightSelectionPixel - sel.getRight();

            // Don't scroll more than half the width of the list
            int halfHorizontalSpace = (childrenRight - childrenLeft) / 2;
            int offset = Math.min(spaceAbove, spaceBelow);
            offset = Math.min(offset, halfHorizontalSpace);

            // Offset the selected item to get it into view
            sel.offsetLeftAndRight(offset);
        }

        // Fill in views above and below
        fillToLeftAndRight(sel, selectedPosition);
    } else {

        int oldLeft = oldSel.getLeft();

        /*
         * Case 3: Staying still
         */
        sel = makeAndAddView(selectedPosition, oldLeft, true, mListPadding.top, true);

        // We're staying still...
        if (oldLeft < childrenLeft) {
            // ... but the top of the old selection was off screen.
            // (This can happen if the data changes size out from under us)
            int newRight = sel.getRight();
            if (newRight < childrenLeft + 20) {
                // Not enough visible -- bring it onscreen
                sel.offsetLeftAndRight(childrenLeft - sel.getLeft());
            }
        }

        // Fill in views above and below
        fillToLeftAndRight(sel, selectedPosition);
    }

    return sel;
}

From source file:com.cxsplay.wallyskim.widget.flexbox.FlexboxLayout.java

/**
 * Sub method for {@link #onLayout(boolean, int, int, int, int)} when the
 * {@link #mFlexDirection} is either {@link #FLEX_DIRECTION_ROW} or
 * {@link #FLEX_DIRECTION_ROW_REVERSE}./*from  w  ww  .j av a 2 s . c  o  m*/
 *
 * @param isRtl  {@code true} if the horizontal layout direction is right to left, {@code
 *               false} otherwise.
 * @param left   the left position of this View
 * @param top    the top position of this View
 * @param right  the right position of this View
 * @param bottom the bottom position of this View
 * @see #getFlexWrap()
 * @see #setFlexWrap(int)
 * @see #getJustifyContent()
 * @see #setJustifyContent(int)
 * @see #getAlignItems()
 * @see #setAlignItems(int)
 * @see LayoutParams#alignSelf
 */
private void layoutHorizontal(boolean isRtl, int left, int top, int right, int bottom) {
    int paddingLeft = getPaddingLeft();
    int paddingRight = getPaddingRight();
    // Use float to reduce the round error that may happen in when justifyContent ==
    // SPACE_BETWEEN or SPACE_AROUND
    float childLeft;
    int currentViewIndex = 0;

    int height = bottom - top;
    int width = right - left;
    // childBottom is used if the mFlexWrap is FLEX_WRAP_WRAP_REVERSE otherwise
    // childTop is used to align the vertical position of the children views.
    int childBottom = height - getPaddingBottom();
    int childTop = getPaddingTop();

    // Used only for RTL layout
    // Use float to reduce the round error that may happen in when justifyContent ==
    // SPACE_BETWEEN or SPACE_AROUND
    float childRight;
    for (int i = 0, size = mFlexLines.size(); i < size; i++) {
        FlexLine flexLine = mFlexLines.get(i);
        if (hasDividerBeforeFlexLine(i)) {
            childBottom -= mDividerHorizontalHeight;
            childTop += mDividerHorizontalHeight;
        }
        float spaceBetweenItem = 0f;
        switch (mJustifyContent) {
        case JUSTIFY_CONTENT_FLEX_START:
            childLeft = paddingLeft;
            childRight = width - paddingRight;
            break;
        case JUSTIFY_CONTENT_FLEX_END:
            childLeft = width - flexLine.mMainSize + paddingRight;
            childRight = flexLine.mMainSize - paddingLeft;
            break;
        case JUSTIFY_CONTENT_CENTER:
            childLeft = paddingLeft + (width - flexLine.mMainSize) / 2f;
            childRight = width - paddingRight - (width - flexLine.mMainSize) / 2f;
            break;
        case JUSTIFY_CONTENT_SPACE_AROUND:
            int visibleCount = flexLine.getItemCountNotGone();
            if (visibleCount != 0) {
                spaceBetweenItem = (width - flexLine.mMainSize) / (float) visibleCount;
            }
            childLeft = paddingLeft + spaceBetweenItem / 2f;
            childRight = width - paddingRight - spaceBetweenItem / 2f;
            break;
        case JUSTIFY_CONTENT_SPACE_BETWEEN:
            childLeft = paddingLeft;
            int visibleItem = flexLine.getItemCountNotGone();
            float denominator = visibleItem != 1 ? visibleItem - 1 : 1f;
            spaceBetweenItem = (width - flexLine.mMainSize) / denominator;
            childRight = width - paddingRight;
            break;
        default:
            throw new IllegalStateException("Invalid justifyContent is set: " + mJustifyContent);
        }
        spaceBetweenItem = Math.max(spaceBetweenItem, 0);

        for (int j = 0; j < flexLine.mItemCount; j++) {
            View child = getReorderedChildAt(currentViewIndex);
            if (child == null) {
                continue;
            } else if (child.getVisibility() == View.GONE) {
                currentViewIndex++;
                continue;
            }
            LayoutParams lp = ((LayoutParams) child.getLayoutParams());
            childLeft += lp.leftMargin;
            childRight -= lp.rightMargin;
            if (hasDividerBeforeChildAtAlongMainAxis(currentViewIndex, j)) {
                childLeft += mDividerVerticalWidth;
                childRight -= mDividerVerticalWidth;
            }

            if (mFlexWrap == FLEX_WRAP_WRAP_REVERSE) {
                if (isRtl) {
                    layoutSingleChildHorizontal(child, flexLine, mFlexWrap, mAlignItems,
                            Math.round(childRight) - child.getMeasuredWidth(),
                            childBottom - child.getMeasuredHeight(), Math.round(childRight), childBottom);
                } else {
                    layoutSingleChildHorizontal(child, flexLine, mFlexWrap, mAlignItems, Math.round(childLeft),
                            childBottom - child.getMeasuredHeight(),
                            Math.round(childLeft) + child.getMeasuredWidth(), childBottom);
                }
            } else {
                if (isRtl) {
                    layoutSingleChildHorizontal(child, flexLine, mFlexWrap, mAlignItems,
                            Math.round(childRight) - child.getMeasuredWidth(), childTop, Math.round(childRight),
                            childTop + child.getMeasuredHeight());
                } else {
                    layoutSingleChildHorizontal(child, flexLine, mFlexWrap, mAlignItems, Math.round(childLeft),
                            childTop, Math.round(childLeft) + child.getMeasuredWidth(),
                            childTop + child.getMeasuredHeight());
                }
            }
            childLeft += child.getMeasuredWidth() + spaceBetweenItem + lp.rightMargin;
            childRight -= child.getMeasuredWidth() + spaceBetweenItem + lp.leftMargin;
            currentViewIndex++;

            flexLine.mLeft = Math.min(flexLine.mLeft, child.getLeft() - lp.leftMargin);
            flexLine.mTop = Math.min(flexLine.mTop, child.getTop() - lp.topMargin);
            flexLine.mRight = Math.max(flexLine.mRight, child.getRight() + lp.rightMargin);
            flexLine.mBottom = Math.max(flexLine.mBottom, child.getBottom() + lp.bottomMargin);
        }
        childTop += flexLine.mCrossSize;
        childBottom -= flexLine.mCrossSize;
    }
}

From source file:com.cxsplay.wallyskim.widget.flexbox.FlexboxLayout.java

/**
 * Sub method for {@link #onLayout(boolean, int, int, int, int)} when the
 * {@link #mFlexDirection} is either {@link #FLEX_DIRECTION_COLUMN} or
 * {@link #FLEX_DIRECTION_COLUMN_REVERSE}.
 *
 * @param isRtl           {@code true} if the horizontal layout direction is right to left,
 *                        {@code false}//w ww .j av  a 2s  . c  o  m
 *                        otherwise
 * @param fromBottomToTop {@code true} if the layout direction is bottom to top, {@code false}
 *                        otherwise
 * @param left            the left position of this View
 * @param top             the top position of this View
 * @param right           the right position of this View
 * @param bottom          the bottom position of this View
 * @see #getFlexWrap()
 * @see #setFlexWrap(int)
 * @see #getJustifyContent()
 * @see #setJustifyContent(int)
 * @see #getAlignItems()
 * @see #setAlignItems(int)
 * @see LayoutParams#alignSelf
 */
private void layoutVertical(boolean isRtl, boolean fromBottomToTop, int left, int top, int right, int bottom) {
    int paddingTop = getPaddingTop();
    int paddingBottom = getPaddingBottom();

    int paddingRight = getPaddingRight();
    int childLeft = getPaddingLeft();
    int currentViewIndex = 0;

    int width = right - left;
    int height = bottom - top;
    // childRight is used if the mFlexWrap is FLEX_WRAP_WRAP_REVERSE otherwise
    // childLeft is used to align the horizontal position of the children views.
    int childRight = width - paddingRight;

    // Use float to reduce the round error that may happen in when justifyContent ==
    // SPACE_BETWEEN or SPACE_AROUND
    float childTop;

    // Used only for if the direction is from bottom to top
    float childBottom;

    for (int i = 0, size = mFlexLines.size(); i < size; i++) {
        FlexLine flexLine = mFlexLines.get(i);
        if (hasDividerBeforeFlexLine(i)) {
            childLeft += mDividerVerticalWidth;
            childRight -= mDividerVerticalWidth;
        }
        float spaceBetweenItem = 0f;
        switch (mJustifyContent) {
        case JUSTIFY_CONTENT_FLEX_START:
            childTop = paddingTop;
            childBottom = height - paddingBottom;
            break;
        case JUSTIFY_CONTENT_FLEX_END:
            childTop = height - flexLine.mMainSize + paddingBottom;
            childBottom = flexLine.mMainSize - paddingTop;
            break;
        case JUSTIFY_CONTENT_CENTER:
            childTop = paddingTop + (height - flexLine.mMainSize) / 2f;
            childBottom = height - paddingBottom - (height - flexLine.mMainSize) / 2f;
            break;
        case JUSTIFY_CONTENT_SPACE_AROUND:
            int visibleCount = flexLine.getItemCountNotGone();
            if (visibleCount != 0) {
                spaceBetweenItem = (height - flexLine.mMainSize) / (float) visibleCount;
            }
            childTop = paddingTop + spaceBetweenItem / 2f;
            childBottom = height - paddingBottom - spaceBetweenItem / 2f;
            break;
        case JUSTIFY_CONTENT_SPACE_BETWEEN:
            childTop = paddingTop;
            int visibleItem = flexLine.getItemCountNotGone();
            float denominator = visibleItem != 1 ? visibleItem - 1 : 1f;
            spaceBetweenItem = (height - flexLine.mMainSize) / denominator;
            childBottom = height - paddingBottom;
            break;
        default:
            throw new IllegalStateException("Invalid justifyContent is set: " + mJustifyContent);
        }
        spaceBetweenItem = Math.max(spaceBetweenItem, 0);

        for (int j = 0; j < flexLine.mItemCount; j++) {
            View child = getReorderedChildAt(currentViewIndex);
            if (child == null) {
                continue;
            } else if (child.getVisibility() == View.GONE) {
                currentViewIndex++;
                continue;
            }
            LayoutParams lp = ((LayoutParams) child.getLayoutParams());
            childTop += lp.topMargin;
            childBottom -= lp.bottomMargin;
            if (hasDividerBeforeChildAtAlongMainAxis(currentViewIndex, j)) {
                childTop += mDividerHorizontalHeight;
                childBottom -= mDividerHorizontalHeight;
            }
            if (isRtl) {
                if (fromBottomToTop) {
                    layoutSingleChildVertical(child, flexLine, true, mAlignItems,
                            childRight - child.getMeasuredWidth(),
                            Math.round(childBottom) - child.getMeasuredHeight(), childRight,
                            Math.round(childBottom));
                } else {
                    layoutSingleChildVertical(child, flexLine, true, mAlignItems,
                            childRight - child.getMeasuredWidth(), Math.round(childTop), childRight,
                            Math.round(childTop) + child.getMeasuredHeight());
                }
            } else {
                if (fromBottomToTop) {
                    layoutSingleChildVertical(child, flexLine, false, mAlignItems, childLeft,
                            Math.round(childBottom) - child.getMeasuredHeight(),
                            childLeft + child.getMeasuredWidth(), Math.round(childBottom));
                } else {
                    layoutSingleChildVertical(child, flexLine, false, mAlignItems, childLeft,
                            Math.round(childTop), childLeft + child.getMeasuredWidth(),
                            Math.round(childTop) + child.getMeasuredHeight());
                }
            }
            childTop += child.getMeasuredHeight() + spaceBetweenItem + lp.bottomMargin;
            childBottom -= child.getMeasuredHeight() + spaceBetweenItem + lp.topMargin;
            currentViewIndex++;

            flexLine.mLeft = Math.min(flexLine.mLeft, child.getLeft() - lp.leftMargin);
            flexLine.mTop = Math.min(flexLine.mTop, child.getTop() - lp.topMargin);
            flexLine.mRight = Math.max(flexLine.mRight, child.getRight() + lp.rightMargin);
            flexLine.mBottom = Math.max(flexLine.mBottom, child.getBottom() + lp.bottomMargin);
        }
        childLeft += flexLine.mCrossSize;
        childRight -= flexLine.mCrossSize;
    }
}

From source file:self.philbrown.droidQuery.$.java

/**
 * Get the coordinates of the {@code view}, relative to the offset parent
 * @param view//from  www .j  a  va2  s . c  o  m
 * @return the coordinates of the given view, relative to the offset parent
 */
private Point position(View view) {
    return new Point(view.getLeft(), view.getTop());
}