Example usage for android.view View offsetLeftAndRight

List of usage examples for android.view View offsetLeftAndRight

Introduction

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

Prototype

public void offsetLeftAndRight(int offset) 

Source Link

Document

Offset this view's horizontal location by the specified amount of pixels.

Usage

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

/**
 * Fills the grid based on positioning the new selection at a specific location. The selection may be moved so that it does not
 * intersect the faded edges. The grid is then filled upwards and downwards from there.
 * //from  w  w  w .  ja va 2  s.  c  o m
 * @param selectedLeft
 *           Where the selected item should be
 * @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 fillFromSelection(int selectedLeft, int childrenLeft, int childrenRight) {
    int fadingEdgeLength = getHorizontalFadingEdgeLength();
    final int selectedPosition = mSelectedPosition;

    View sel;

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

    sel = makeAndAddView(selectedPosition, selectedLeft, 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
        final int spaceBefore = sel.getLeft() - leftSelectionPixel;

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

        // Now offset the selected item to get it into view
        sel.offsetLeftAndRight(-offset);
    } else if (sel.getLeft() < leftSelectionPixel) {
        // Find space required to bring the top of the selected item fully
        // into view
        final int spaceBefore = leftSelectionPixel - sel.getLeft();

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

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

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

    if (!mStackFromRight) {
        correctTooWide(getChildCount());
    } else {
        correctTooSmall(getChildCount());
    }

    return sel;
}

From source file:com.viewpagerindicator.MyDirectionalViewPager.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.//w  w  w. j a v a  2  s . co m
 *
 * @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) {
        if (mOrientation == HORIZONTAL) {
            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 hgrav = lp.gravity & Gravity.HORIZONTAL_GRAVITY_MASK;
                int childTop = 0;
                switch (hgrav) {
                default:
                    childTop = paddingTop;
                    break;
                case Gravity.LEFT:
                    childTop = paddingTop;
                    paddingTop += child.getWidth();
                    break;
                case Gravity.CENTER_HORIZONTAL:
                    childTop = Math.max((height - child.getMeasuredWidth()) / 2, paddingTop);
                    break;
                case Gravity.RIGHT:
                    childTop = height - paddingBottom - child.getMeasuredWidth();
                    paddingTop += child.getMeasuredWidth();
                    break;
                }
                childTop += scrollY;

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

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

    if (mPageTransformer != null) {
        final int scrollX = getScrollX();
        final int scrollY = 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;
            final float transformPos;
            if (mOrientation == HORIZONTAL) {
                transformPos = (float) (child.getLeft() - scrollX) / getClientWidth();
            } else {
                transformPos = (float) (child.getTop() - scrollY) / getClientHeight();
            }
            mPageTransformer.transformPage(child, transformPos);
        }
    }

    mCalledSuper = true;
}

From source file:com.guide.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   ww w .  ja  v a  2 s .  co  m
 * @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:dev.dworks.libs.widget.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  ww w . ja v  a  2  s.  c  o  m*/
 *
 * @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 (mOrientation == HORIZONTAL) {
        if (mDecorChildCount > 0) {
            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 {
        if (mDecorChildCount > 0) {
            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 (mSeenPositionMin < 0 || position < mSeenPositionMin) {
            mSeenPositionMin = position;
        }
        if (mSeenPositionMax < 0 || FloatMath.ceil(position + offset) > mSeenPositionMax) {
            mSeenPositionMax = position + 1;
        }

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

        if (mPageTransformer != null) {
            final int scrollY = 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;

                final float transformPos = (float) (child.getTop() - scrollY) / getHeight();
                mPageTransformer.transformPage(child, transformPos);
            }
        }
    }
    mCalledSuper = true;
}

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

/**
 * Add a view as a child and make sure it is measured (if necessary) and
 * positioned properly./*ww w.  j a  v  a2s  .  c  o m*/
 *
 * @param child The view to add
 * @param position The position of the view
 * @param y The y position relative to which this view will be positioned
 * @param flow if true, align top edge to y. If false, align bottom edge
 *        to y.
 * @param childrenLeft Left edge where children should be positioned
 * @param selected Is this position selected?
 * @param recycled Has this view been pulled from the recycle bin? If so it
 *        does not need to be remeasured.
 * @param where Where to add the item in the list
 *
 */
private void setupChild(View child, int position, int y, boolean flow, int childrenLeft, boolean selected,
        boolean recycled, int where) {
    boolean isSelected = selected && shouldShowSelector();
    final boolean updateChildSelected = isSelected != child.isSelected();
    final int mode = mTouchMode;
    final boolean isPressed = mode > TOUCH_MODE_DOWN && mode < TOUCH_MODE_SCROLL && mMotionPosition == position;
    final boolean updateChildPressed = isPressed != child.isPressed();

    boolean needToMeasure = !recycled || updateChildSelected || child.isLayoutRequested();

    // Respect layout params that are already in the view. Otherwise make
    // some up...
    LayoutParams p = (LayoutParams) child.getLayoutParams();
    if (p == null) {
        p = (LayoutParams) generateDefaultLayoutParams();
    }
    p.viewType = mAdapter.getItemViewType(position);

    if (recycled && !p.forceAdd) {
        attachViewToParent(child, where, p);
    } else {
        p.forceAdd = false;
        addViewInLayout(child, where, p, true);
    }

    if (updateChildSelected) {
        child.setSelected(isSelected);
        if (isSelected) {
            requestFocus();
        }
    }

    if (updateChildPressed) {
        child.setPressed(isPressed);
    }

    if (mChoiceMode != CHOICE_MODE_NONE && mCheckStates != null) {
        if (child instanceof Checkable) {
            ((Checkable) child).setChecked(mCheckStates.get(position));
        } else if (getContext()
                .getApplicationInfo().targetSdkVersion >= android.os.Build.VERSION_CODES.HONEYCOMB) {
            Compat.setActivated(child, mCheckStates.get(position));
        }
    }

    if (needToMeasure) {
        int childHeightSpec = ViewGroup
                .getChildMeasureSpec(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), 0, p.height);

        int childWidthSpec = ViewGroup.getChildMeasureSpec(
                MeasureSpec.makeMeasureSpec(mColumnWidth, MeasureSpec.EXACTLY), 0, p.width);
        child.measure(childWidthSpec, childHeightSpec);
    } else {
        cleanupLayoutState(child);
    }

    final int w = child.getMeasuredWidth();
    final int h = child.getMeasuredHeight();

    int childLeft;
    final int childTop = flow ? y : y - h;

    final int layoutDirection = ViewCompat.getLayoutDirection(this);
    final int absoluteGravity = GravityCompat.getAbsoluteGravity(mGravity, layoutDirection);
    switch (absoluteGravity & Gravity.HORIZONTAL_GRAVITY_MASK) {
    case Gravity.LEFT:
        childLeft = childrenLeft;
        break;
    case Gravity.CENTER_HORIZONTAL:
        childLeft = childrenLeft + ((mColumnWidth - w) / 2);
        break;
    case Gravity.RIGHT:
        childLeft = childrenLeft + mColumnWidth - w;
        break;
    default:
        childLeft = childrenLeft;
        break;
    }

    if (needToMeasure) {
        final int childRight = childLeft + w;
        final int childBottom = childTop + h;
        child.layout(childLeft, childTop, childRight, childBottom);
    } else {
        child.offsetLeftAndRight(childLeft - child.getLeft());
        child.offsetTopAndBottom(childTop - child.getTop());
    }

    if (mCachingStarted) {
        child.setDrawingCacheEnabled(true);
    }

    if (recycled && (((LayoutParams) child.getLayoutParams()).scrappedFromPosition) != position) {
        Compat.jumpDrawablesToCurrentState(child);
    }
}

From source file:com.zhongsou.souyue.ui.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.//  w ww  . j  a 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 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.example.hlist.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./*from w w  w . j  a v  a 2  s.  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 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.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./*from   ww w .  j  a  va  2  s  .  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 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./*from   www . j a va  2  s.  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 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: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 w w w  .j av a2s  . c om*/
 *
 * @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;
}