Example usage for android.view View getTop

List of usage examples for android.view View getTop

Introduction

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

Prototype

@ViewDebug.CapturedViewProperty
public final int getTop() 

Source Link

Document

Top position of this view relative to its parent.

Usage

From source file:com.artifex.mupdf.view.ThumbnailViews.java

/**
 * Handle an arrow scroll going up or down. Take into account whether items
 * are selectable, whether there are focusable items, etc.
 * /*w w w . j av  a  2  s .  c  o  m*/
 * @param direction
 *            either {@link View#FOCUS_UP} or {@link View#FOCUS_DOWN} or
 *            {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT} depending
 *            on the current view orientation.
 * 
 * @return Whether any scrolling, selection or focus change occurred.
 */
private boolean arrowScrollImpl(int direction) {
    forceValidFocusDirection(direction);

    if (getChildCount() <= 0) {
        return false;
    }

    View selectedView = getSelectedView();
    int selectedPos = mSelectedPosition;

    int nextSelectedPosition = lookForSelectablePositionOnScreen(direction);
    int amountToScroll = amountToScroll(direction, nextSelectedPosition);

    // If we are moving focus, we may OVERRIDE the default behaviour
    final ArrowScrollFocusResult focusResult = (mItemsCanFocus ? arrowScrollFocused(direction) : null);
    if (focusResult != null) {
        nextSelectedPosition = focusResult.getSelectedPosition();
        amountToScroll = focusResult.getAmountToScroll();
    }

    boolean needToRedraw = (focusResult != null);
    if (nextSelectedPosition != INVALID_POSITION) {
        handleNewSelectionChange(selectedView, direction, nextSelectedPosition, focusResult != null);

        setSelectedPositionInt(nextSelectedPosition);
        setNextSelectedPositionInt(nextSelectedPosition);

        selectedView = getSelectedView();
        selectedPos = nextSelectedPosition;

        if (mItemsCanFocus && focusResult == null) {
            // There was no new view found to take focus, make sure we
            // don't leave focus with the old selection.
            final View focused = getFocusedChild();
            if (focused != null) {
                focused.clearFocus();
            }
        }

        needToRedraw = true;
        checkSelectionChanged();
    }

    if (amountToScroll > 0) {
        trackMotionScroll(
                direction == View.FOCUS_UP || direction == View.FOCUS_LEFT ? amountToScroll : -amountToScroll);
        needToRedraw = true;
    }

    // If we didn't find a new focusable, make sure any existing focused
    // item that was panned off screen gives up focus.
    if (mItemsCanFocus && focusResult == null && selectedView != null && selectedView.hasFocus()) {
        final View focused = selectedView.findFocus();
        if (!isViewAncestorOf(focused, this) || distanceToView(focused) > 0) {
            focused.clearFocus();
        }
    }

    // If the current selection is panned off, we need to remove the
    // selection
    if (nextSelectedPosition == INVALID_POSITION && selectedView != null
            && !isViewAncestorOf(selectedView, this)) {
        selectedView = null;
        hideSelector();

        // But we don't want to set the ressurect position (that would make
        // subsequent
        // unhandled key events bring back the item we just scrolled off)
        mResurrectToPosition = INVALID_POSITION;
    }

    if (needToRedraw) {
        if (selectedView != null) {
            positionSelector(selectedPos, selectedView);
            mSelectedStart = selectedView.getTop();
        }

        if (!awakenScrollbarsInternal()) {
            invalidate();
        }

        invokeOnItemScrollListener();
        return true;
    }

    return false;
}

From source file:com.artifex.mupdflib.TwoWayView.java

@TargetApi(11)
private void setupChild(View child, int position, int top, int left, boolean flow, boolean selected,
        boolean recycled) {
    final boolean isSelected = selected && shouldShowSelector();
    final boolean updateChildSelected = isSelected != child.isSelected();
    final int touchMode = mTouchMode;

    final boolean isPressed = touchMode > TOUCH_MODE_DOWN && touchMode < TOUCH_MODE_DRAGGING
            && mMotionPosition == position;

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

    // Respect layout params that are already in the view. Otherwise make some up...
    LayoutParams lp = (LayoutParams) child.getLayoutParams();
    if (lp == null) {
        lp = generateDefaultLayoutParams();
    }/*from   www  . j  a va2s.  c o m*/

    lp.viewType = mAdapter.getItemViewType(position);

    if (recycled && !lp.forceAdd) {
        attachViewToParent(child, (flow ? -1 : 0), lp);
    } else {
        lp.forceAdd = false;
        addViewInLayout(child, (flow ? -1 : 0), lp, true);
    }

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

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

    if (mChoiceMode != ChoiceMode.NONE && mCheckStates != null) {
        if (child instanceof Checkable) {
            ((Checkable) child).setChecked(mCheckStates.get(position));
        } else if (Build.VERSION.SDK_INT >= HONEYCOMB) {
            child.setActivated(mCheckStates.get(position));
        }
    }

    if (needToMeasure) {
        measureChild(child, lp);
    } else {
        cleanupLayoutState(child);
    }

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

    final int childTop = (mIsVertical && !flow ? top - h : top);
    final int childLeft = (!mIsVertical && !flow ? left - w : left);

    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());
    }
}

From source file:com.boutline.sports.helpers.TwoWayView.java

private View moveSelection(View oldSelected, View newSelected, int delta, int start, int end) {
    final int selectedPosition = mSelectedPosition;

    final int oldSelectedStart = getChildStartEdge(oldSelected);
    final int oldSelectedEnd = getChildEndEdge(oldSelected);

    View selected = null;//from  w w  w  .j a v  a 2 s  .  c  o  m

    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.
         *    oldSelected = A
         *    selected = B
         */

        // Put oldSelected (A) where it belongs
        oldSelected = makeAndAddView(selectedPosition - 1, oldSelectedStart, true, false);

        final int itemMargin = mItemMargin;

        // Now put the new selection (B) below that
        selected = makeAndAddView(selectedPosition, oldSelectedEnd + itemMargin, true, true);

        final int selectedStart = getChildStartEdge(selected);
        final int selectedEnd = getChildEndEdge(selected);

        // Some of the newly selected item extends below the bottom of the list
        if (selectedEnd > end) {
            // Find space available above the selection into which we can scroll upwards
            final int spaceBefore = selectedStart - start;

            // Find space required to bring the bottom of the selected item fully into view
            final int spaceAfter = selectedEnd - end;

            // Don't scroll more than half the size of the list
            final int halfSpace = (end - start) / 2;
            int offset = Math.min(spaceBefore, spaceAfter);
            offset = Math.min(offset, halfSpace);

            if (mIsVertical) {
                oldSelected.offsetTopAndBottom(-offset);
                selected.offsetTopAndBottom(-offset);
            } else {
                oldSelected.offsetLeftAndRight(-offset);
                selected.offsetLeftAndRight(-offset);
            }
        }

        // Fill in views before and after
        fillBefore(mSelectedPosition - 2, selectedStart - itemMargin);
        adjustViewsStartOrEnd();
        fillAfter(mSelectedPosition + 1, selectedEnd + itemMargin);
    } 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.
         *    newSelected = A
         *    olSelected = B
         */

        if (newSelected != null) {
            // Try to position the top of newSel (A) where it was before it was selected
            final int newSelectedStart = (mIsVertical ? newSelected.getTop() : newSelected.getLeft());
            selected = makeAndAddView(selectedPosition, newSelectedStart, true, true);
        } else {
            // If (A) was not on screen and so did not have a view, position
            // it above the oldSelected (B)
            selected = makeAndAddView(selectedPosition, oldSelectedStart, false, true);
        }

        final int selectedStart = getChildStartEdge(selected);
        final int selectedEnd = getChildEndEdge(selected);

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

            // Find space available below the selection into which we can scroll downwards
            final int spaceAfter = end - selectedEnd;

            // Don't scroll more than half the height of the list
            final int halfSpace = (end - start) / 2;
            int offset = Math.min(spaceBefore, spaceAfter);
            offset = Math.min(offset, halfSpace);

            if (mIsVertical) {
                selected.offsetTopAndBottom(offset);
            } else {
                selected.offsetLeftAndRight(offset);
            }
        }

        // Fill in views above and below
        fillBeforeAndAfter(selected, selectedPosition);
    } else {
        /*
         * Case 3: Staying still
         */

        selected = makeAndAddView(selectedPosition, oldSelectedStart, true, true);

        final int selectedStart = getChildStartEdge(selected);
        final int selectedEnd = getChildEndEdge(selected);

        // We're staying still...
        if (oldSelectedStart < start) {
            // ... but the top of the old selection was off screen.
            // (This can happen if the data changes size out from under us)
            int newEnd = selectedEnd;
            if (newEnd < start + 20) {
                // Not enough visible -- bring it onscreen
                if (mIsVertical) {
                    selected.offsetTopAndBottom(start - selectedStart);
                } else {
                    selected.offsetLeftAndRight(start - selectedStart);
                }
            }
        }

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

    return selected;
}

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

@Override
protected void layoutChildren() {
    final boolean blockLayoutRequests = mBlockLayoutRequests;
    if (!blockLayoutRequests) {
        mBlockLayoutRequests = true;/*from w  ww .jav a 2s . co  m*/
    } else {
        return;
    }

    try {
        super.layoutChildren();

        invalidate();

        if (mAdapter == null) {
            resetList();
            invokeOnItemScrollListener();
            return;
        }

        int childrenTop = mListPadding.top;
        int childrenBottom = getBottom() - getTop() - mListPadding.bottom;

        int childCount = getChildCount();
        int index = 0;
        int delta = 0;

        View sel;
        View oldSel = null;
        View oldFirst = null;
        View newSel = null;

        View focusLayoutRestoreView = null;

        AccessibilityNodeInfo accessibilityFocusLayoutRestoreNode = null;
        View accessibilityFocusLayoutRestoreView = null;
        int accessibilityFocusPosition = INVALID_POSITION;

        // Remember stuff we will need down below
        switch (mLayoutMode) {
        case LAYOUT_SET_SELECTION:
            index = mNextSelectedPosition - mFirstPosition;
            if (index >= 0 && index < childCount) {
                newSel = getChildAt(index);
            }
            break;
        case LAYOUT_FORCE_TOP:
        case LAYOUT_FORCE_BOTTOM:
        case LAYOUT_SPECIFIC:
        case LAYOUT_SYNC:
            break;
        case LAYOUT_MOVE_SELECTION:
        default:
            // Remember the previously selected view
            index = mSelectedPosition - mFirstPosition;
            if (index >= 0 && index < childCount) {
                oldSel = getChildAt(index);
            }

            // Remember the previous first child
            oldFirst = getChildAt(0);

            if (mNextSelectedPosition >= 0) {
                delta = mNextSelectedPosition - mSelectedPosition;
            }

            // Caution: newSel might be null
            newSel = getChildAt(index + delta);
        }

        boolean dataChanged = mDataChanged;
        if (dataChanged) {
            handleDataChanged();
        }

        // Handle the empty set by removing all views that are visible
        // and calling it a day
        if (mItemCount == 0) {
            resetList();
            invokeOnItemScrollListener();
            return;
        } else if (mItemCount != mAdapter.getCount()) {
            throw new IllegalStateException("The content of the adapter has changed but "
                    + "ListView did not receive a notification. Make sure the content of "
                    + "your adapter is not modified from a background thread, but only "
                    + "from the UI thread. [in ListView(" + getId() + ", " + getClass() + ") with Adapter("
                    + mAdapter.getClass() + ")]");
        }

        setSelectedPositionInt(mNextSelectedPosition);

        // Pull all children into the RecycleBin.
        // These views will be reused if possible
        final int firstPosition = mFirstPosition;
        final RecycleBin recycleBin = mRecycler;

        // reset the focus restoration
        View focusLayoutRestoreDirectChild = null;

        // Don't put header or footer views into the Recycler. Those are
        // already cached in mHeaderViews;
        if (dataChanged) {
            for (int i = 0; i < childCount; i++) {
                recycleBin.addScrapView(getChildAt(i), firstPosition + i);
            }
        } else {
            recycleBin.fillActiveViews(childCount, firstPosition);
        }

        // take focus back to us temporarily to avoid the eventual
        // call to clear focus when removing the focused child below
        // from messing things up when ViewAncestor assigns focus back
        // to someone else
        final View focusedChild = getFocusedChild();
        if (focusedChild != null) {
            // TODO: in some cases focusedChild.getParent() == null

            // we can remember the focused view to restore after relayout if the
            // data hasn't changed, or if the focused position is a header or footer
            if (!dataChanged || isDirectChildHeaderOrFooter(focusedChild)) {
                focusLayoutRestoreDirectChild = focusedChild;
                // remember the specific view that had focus
                focusLayoutRestoreView = findFocus();
                if (focusLayoutRestoreView != null) {
                    // tell it we are going to mess with it
                    focusLayoutRestoreView.onStartTemporaryDetach();
                }
            }
            requestFocus();
        }

        // TODO We can use those accessability methods, we just remove those (j.m.)
        //            //Remember which child, if any, had accessibility focus.
        //            final ViewRootImpl viewRootImpl = getViewRootImpl();
        //            if (viewRootImpl != null) {
        //                final View accessFocusedView = viewRootImpl.getAccessibilityFocusedHost();
        //                if (accessFocusedView != null) {
        //                    final View accessFocusedChild = findAccessibilityFocusedChild(
        //                            accessFocusedView);
        //                    if (accessFocusedChild != null) {
        //                        if (!dataChanged || isDirectChildHeaderOrFooter(accessFocusedChild)) {
        //                            // If the views won't be changing, try to maintain
        //                            // focus on the current view host and (if
        //                            // applicable) its virtual view.
        //                            accessibilityFocusLayoutRestoreView = accessFocusedView;
        //                            accessibilityFocusLayoutRestoreNode = viewRootImpl
        //                                    .getAccessibilityFocusedVirtualView();
        //                        } else {
        //                            // Otherwise, try to maintain focus at the same
        //                            // position.
        //                            accessibilityFocusPosition = getPositionForView(accessFocusedChild);
        //                        }
        //                    }
        //                }
        //            }

        // Clear out old views
        detachAllViewsFromParent();
        recycleBin.removeSkippedScrap();

        switch (mLayoutMode) {
        case LAYOUT_SET_SELECTION:
            if (newSel != null) {
                sel = fillFromSelection(newSel.getTop(), childrenTop, childrenBottom);
            } else {
                sel = fillFromMiddle(childrenTop, childrenBottom);
            }
            break;
        case LAYOUT_SYNC:
            sel = fillSpecific(mSyncPosition, mSpecificTop);
            break;
        case LAYOUT_FORCE_BOTTOM:
            sel = fillUp(mItemCount - 1, childrenBottom);
            adjustViewsUpOrDown();
            break;
        case LAYOUT_FORCE_TOP:
            mFirstPosition = 0;
            sel = fillFromTop(childrenTop);
            adjustViewsUpOrDown();
            break;
        case LAYOUT_SPECIFIC:
            sel = fillSpecific(reconcileSelectedPosition(), mSpecificTop);
            break;
        case LAYOUT_MOVE_SELECTION:
            sel = moveSelection(oldSel, newSel, delta, childrenTop, childrenBottom);
            break;
        default:
            if (childCount == 0) {
                if (!mStackFromBottom) {
                    final int position = lookForSelectablePosition(0, true);
                    setSelectedPositionInt(position);
                    sel = fillFromTop(childrenTop);
                } else {
                    final int position = lookForSelectablePosition(mItemCount - 1, false);
                    setSelectedPositionInt(position);
                    sel = fillUp(mItemCount - 1, childrenBottom);
                }
            } else {
                if (mSelectedPosition >= 0 && mSelectedPosition < mItemCount) {
                    sel = fillSpecific(mSelectedPosition, oldSel == null ? childrenTop : oldSel.getTop());
                } else if (mFirstPosition < mItemCount) {
                    sel = fillSpecific(mFirstPosition, oldFirst == null ? childrenTop : oldFirst.getTop());
                } else {
                    sel = fillSpecific(0, childrenTop);
                }
            }
            break;
        }

        // Flush any cached views that did not get reused above
        recycleBin.scrapActiveViews();

        if (sel != null) {
            // the current selected item should get focus if items
            // are focusable
            if (mItemsCanFocus && hasFocus() && !sel.hasFocus()) {
                final boolean focusWasTaken = (sel == focusLayoutRestoreDirectChild
                        && focusLayoutRestoreView != null && focusLayoutRestoreView.requestFocus())
                        || sel.requestFocus();
                if (!focusWasTaken) {
                    // selected item didn't take focus, fine, but still want
                    // to make sure something else outside of the selected view
                    // has focus
                    final View focused = getFocusedChild();
                    if (focused != null) {
                        focused.clearFocus();
                    }
                    positionSelector(INVALID_POSITION, sel);
                } else {
                    sel.setSelected(false);
                    mSelectorRect.setEmpty();
                }
            } else {
                positionSelector(INVALID_POSITION, sel);
            }
            mSelectedTop = sel.getTop();
        } else {
            if (mTouchMode > TOUCH_MODE_DOWN && mTouchMode < TOUCH_MODE_SCROLL) {
                View child = getChildAt(mMotionPosition - mFirstPosition);
                if (child != null)
                    positionSelector(mMotionPosition, child);
            } else {
                mSelectedTop = 0;
                mSelectorRect.setEmpty();
            }

            // even if there is not selected position, we may need to restore
            // focus (i.e. something focusable in touch mode)
            if (hasFocus() && focusLayoutRestoreView != null) {
                focusLayoutRestoreView.requestFocus();
            }
        }
        // TODO We can use those accessability methods, we just remove those (j.m.)
        //            // Attempt to restore accessibility focus.
        //            if (accessibilityFocusLayoutRestoreNode != null) {
        //                accessibilityFocusLayoutRestoreNode.performAction(
        //                        AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS);
        //            } else if (accessibilityFocusLayoutRestoreView != null) {
        //                accessibilityFocusLayoutRestoreView.requestAccessibilityFocus();
        //            } else if (accessibilityFocusPosition != INVALID_POSITION) {
        //                // Bound the position within the visible children.
        //                final int position = MathUtils.constrain(
        //                        (accessibilityFocusPosition - mFirstPosition), 0, (getChildCount() - 1));
        //                final View restoreView = getChildAt(position);
        //                if (restoreView != null) {
        //                    restoreView.requestAccessibilityFocus();
        //                }
        //            }

        // tell focus view we are done mucking with it, if it is still in
        // our view hierarchy.
        if (focusLayoutRestoreView != null && focusLayoutRestoreView.getWindowToken() != null) {
            focusLayoutRestoreView.onFinishTemporaryDetach();
        }

        mLayoutMode = LAYOUT_NORMAL;
        mDataChanged = false;
        if (mPositionScrollAfterLayout != null) {
            post(mPositionScrollAfterLayout);
            mPositionScrollAfterLayout = null;
        }
        mNeedSync = false;
        setNextSelectedPositionInt(mSelectedPosition);

        updateScrollIndicators();

        if (mItemCount > 0) {
            checkSelectionChanged();
        }

        invokeOnItemScrollListener();
    } finally {
        if (!blockLayoutRequests) {
            mBlockLayoutRequests = false;
        }
    }
}

From source file:com.datarita.ultimatecamera.turu.views.TwoWayView.java

private void layoutChildren() {
    if (getWidth() == 0 || getHeight() == 0) {
        return;//from  w w w.ja v  a  2s  . co m
    }

    final boolean blockLayoutRequests = mBlockLayoutRequests;
    if (!blockLayoutRequests) {
        mBlockLayoutRequests = true;
    } else {
        return;
    }

    try {
        invalidate();

        if (mAdapter == null) {
            resetState();
            return;
        }

        final int start = (mIsVertical ? getPaddingTop() : getPaddingLeft());
        final int end = (mIsVertical ? getHeight() - getPaddingBottom() : getWidth() - getPaddingRight());

        int childCount = getChildCount();
        int index = 0;
        int delta = 0;

        View focusLayoutRestoreView = null;

        View selected = null;
        View oldSelected = null;
        View newSelected = null;
        View oldFirstChild = null;

        switch (mLayoutMode) {
        case LAYOUT_SET_SELECTION:
            index = mNextSelectedPosition - mFirstPosition;
            if (index >= 0 && index < childCount) {
                newSelected = getChildAt(index);
            }

            break;

        case LAYOUT_FORCE_TOP:
        case LAYOUT_FORCE_BOTTOM:
        case LAYOUT_SPECIFIC:
        case LAYOUT_SYNC:
            break;

        case LAYOUT_MOVE_SELECTION:
        default:
            // Remember the previously selected view
            index = mSelectedPosition - mFirstPosition;
            if (index >= 0 && index < childCount) {
                oldSelected = getChildAt(index);
            }

            // Remember the previous first child
            oldFirstChild = getChildAt(0);

            if (mNextSelectedPosition >= 0) {
                delta = mNextSelectedPosition - mSelectedPosition;
            }

            // Caution: newSelected might be null
            newSelected = getChildAt(index + delta);
        }

        final boolean dataChanged = mDataChanged;
        if (dataChanged) {
            handleDataChanged();
        }

        // Handle the empty set by removing all views that are visible
        // and calling it a day
        if (mItemCount == 0) {
            resetState();
            return;
        } else if (mItemCount != mAdapter.getCount()) {
            throw new IllegalStateException("The content of the mAdapter has changed but "
                    + "TwoWayView did not receive a notification. Make sure the content of "
                    + "your mAdapter is not modified from a background thread, but only "
                    + "from the UI thread. [in TwoWayView(" + getId() + ", " + getClass() + ") with Adapter("
                    + mAdapter.getClass() + ")]");
        }

        setSelectedPositionInt(mNextSelectedPosition);

        // Reset the focus restoration
        View focusLayoutRestoreDirectChild = null;

        // Pull all children into the RecycleBin.
        // These views will be reused if possible
        final int firstPosition = mFirstPosition;
        final RecycleBin recycleBin = mRecycler;

        if (dataChanged) {
            for (int i = 0; i < childCount; i++) {
                recycleBin.addScrapView(getChildAt(i), firstPosition + i);
            }
        } else {
            recycleBin.fillActiveViews(childCount, firstPosition);
        }

        // Take focus back to us temporarily to avoid the eventual
        // call to clear focus when removing the focused child below
        // from messing things up when ViewAncestor assigns focus back
        // to someone else.
        final View focusedChild = getFocusedChild();
        if (focusedChild != null) {
            // We can remember the focused view to restore after relayout if the
            // data hasn't changed, or if the focused position is a header or footer.
            if (!dataChanged) {
                focusLayoutRestoreDirectChild = focusedChild;

                // Remember the specific view that had focus
                focusLayoutRestoreView = findFocus();
                if (focusLayoutRestoreView != null) {
                    // Tell it we are going to mess with it
                    focusLayoutRestoreView.onStartTemporaryDetach();
                }
            }

            requestFocus();
        }

        // FIXME: We need a way to save current accessibility focus here
        // so that it can be restored after we re-attach the children on each
        // layout round.

        detachAllViewsFromParent();

        switch (mLayoutMode) {
        case LAYOUT_SET_SELECTION:
            if (newSelected != null) {
                final int newSelectedStart = (mIsVertical ? newSelected.getTop() : newSelected.getLeft());

                selected = fillFromSelection(newSelectedStart, start, end);
            } else {
                selected = fillFromMiddle(start, end);
            }

            break;

        case LAYOUT_SYNC:
            selected = fillSpecific(mSyncPosition, mSpecificStart);
            break;

        case LAYOUT_FORCE_BOTTOM:
            selected = fillBefore(mItemCount - 1, end);
            adjustViewsStartOrEnd();
            break;

        case LAYOUT_FORCE_TOP:
            mFirstPosition = 0;
            selected = fillFromOffset(start);
            adjustViewsStartOrEnd();
            break;

        case LAYOUT_SPECIFIC:
            selected = fillSpecific(reconcileSelectedPosition(), mSpecificStart);
            break;

        case LAYOUT_MOVE_SELECTION:
            selected = moveSelection(oldSelected, newSelected, delta, start, end);
            break;

        default:
            if (childCount == 0) {
                final int position = lookForSelectablePosition(0);
                setSelectedPositionInt(position);
                selected = fillFromOffset(start);
            } else {
                if (mSelectedPosition >= 0 && mSelectedPosition < mItemCount) {
                    int offset = start;
                    if (oldSelected != null) {
                        offset = (mIsVertical ? oldSelected.getTop() : oldSelected.getLeft());
                    }
                    selected = fillSpecific(mSelectedPosition, offset);
                } else if (mFirstPosition < mItemCount) {
                    int offset = start;
                    if (oldFirstChild != null) {
                        offset = (mIsVertical ? oldFirstChild.getTop() : oldFirstChild.getLeft());
                    }

                    selected = fillSpecific(mFirstPosition, offset);
                } else {
                    selected = fillSpecific(0, start);
                }
            }

            break;

        }

        recycleBin.scrapActiveViews();

        if (selected != null) {
            if (mItemsCanFocus && hasFocus() && !selected.hasFocus()) {
                final boolean focusWasTaken = (selected == focusLayoutRestoreDirectChild
                        && focusLayoutRestoreView != null && focusLayoutRestoreView.requestFocus())
                        || selected.requestFocus();

                if (!focusWasTaken) {
                    // Selected item didn't take focus, fine, but still want
                    // to make sure something else outside of the selected view
                    // has focus
                    final View focused = getFocusedChild();
                    if (focused != null) {
                        focused.clearFocus();
                    }

                    positionSelector(INVALID_POSITION, selected);
                } else {
                    selected.setSelected(false);
                    mSelectorRect.setEmpty();
                }
            } else {
                positionSelector(INVALID_POSITION, selected);
            }

            mSelectedStart = (mIsVertical ? selected.getTop() : selected.getLeft());
        } else {
            if (mTouchMode > TOUCH_MODE_DOWN && mTouchMode < TOUCH_MODE_DRAGGING) {
                View child = getChildAt(mMotionPosition - mFirstPosition);

                if (child != null) {
                    positionSelector(mMotionPosition, child);
                }
            } else {
                mSelectedStart = 0;
                mSelectorRect.setEmpty();
            }

            // Even if there is not selected position, we may need to restore
            // focus (i.e. something focusable in touch mode)
            if (hasFocus() && focusLayoutRestoreView != null) {
                focusLayoutRestoreView.requestFocus();
            }
        }

        // Tell focus view we are done mucking with it, if it is still in
        // our view hierarchy.
        if (focusLayoutRestoreView != null && focusLayoutRestoreView.getWindowToken() != null) {
            focusLayoutRestoreView.onFinishTemporaryDetach();
        }

        mLayoutMode = LAYOUT_NORMAL;
        mDataChanged = false;
        mNeedSync = false;

        setNextSelectedPositionInt(mSelectedPosition);
        if (mItemCount > 0) {
            checkSelectionChanged();
        }

        invokeOnItemScrollListener();
    } finally {
        if (!blockLayoutRequests) {
            mBlockLayoutRequests = false;
            mDataChanged = false;
        }
    }
}

From source file:com.boutline.sports.helpers.TwoWayView.java

private void layoutChildren() {
    if (getWidth() == 0 || getHeight() == 0) {
        return;//from  w  ww.  j  a  v a 2s .c om
    }

    final boolean blockLayoutRequests = mBlockLayoutRequests;
    if (!blockLayoutRequests) {
        mBlockLayoutRequests = true;
    } else {
        return;
    }

    try {
        invalidate();

        if (mAdapter == null) {
            resetState();
            return;
        }

        final int start = getStartEdge();
        final int end = getEndEdge();

        int childCount = getChildCount();
        int index = 0;
        int delta = 0;

        View focusLayoutRestoreView = null;

        View selected = null;
        View oldSelected = null;
        View newSelected = null;
        View oldFirstChild = null;

        switch (mLayoutMode) {
        case LAYOUT_SET_SELECTION:
            index = mNextSelectedPosition - mFirstPosition;
            if (index >= 0 && index < childCount) {
                newSelected = getChildAt(index);
            }

            break;

        case LAYOUT_FORCE_TOP:
        case LAYOUT_FORCE_BOTTOM:
        case LAYOUT_SPECIFIC:
        case LAYOUT_SYNC:
            break;

        case LAYOUT_MOVE_SELECTION:
        default:
            // Remember the previously selected view
            index = mSelectedPosition - mFirstPosition;
            if (index >= 0 && index < childCount) {
                oldSelected = getChildAt(index);
            }

            // Remember the previous first child
            oldFirstChild = getChildAt(0);

            if (mNextSelectedPosition >= 0) {
                delta = mNextSelectedPosition - mSelectedPosition;
            }

            // Caution: newSelected might be null
            newSelected = getChildAt(index + delta);
        }

        final boolean dataChanged = mDataChanged;
        if (dataChanged) {
            handleDataChanged();
        }

        // Handle the empty set by removing all views that are visible
        // and calling it a day
        if (mItemCount == 0) {
            resetState();
            return;
        } else if (mItemCount != mAdapter.getCount()) {
            throw new IllegalStateException("The content of the adapter has changed but "
                    + "TwoWayView did not receive a notification. Make sure the content of "
                    + "your adapter is not modified from a background thread, but only "
                    + "from the UI thread. [in TwoWayView(" + getId() + ", " + getClass() + ") with Adapter("
                    + mAdapter.getClass() + ")]");
        }

        setSelectedPositionInt(mNextSelectedPosition);

        // Reset the focus restoration
        View focusLayoutRestoreDirectChild = null;

        // Pull all children into the RecycleBin.
        // These views will be reused if possible
        final int firstPosition = mFirstPosition;
        final RecycleBin recycleBin = mRecycler;

        if (dataChanged) {
            for (int i = 0; i < childCount; i++) {
                recycleBin.addScrapView(getChildAt(i), firstPosition + i);
            }
        } else {
            recycleBin.fillActiveViews(childCount, firstPosition);
        }

        // Take focus back to us temporarily to avoid the eventual
        // call to clear focus when removing the focused child below
        // from messing things up when ViewAncestor assigns focus back
        // to someone else.
        final View focusedChild = getFocusedChild();
        if (focusedChild != null) {
            // We can remember the focused view to restore after relayout if the
            // data hasn't changed, or if the focused position is a header or footer.
            if (!dataChanged) {
                focusLayoutRestoreDirectChild = focusedChild;

                // Remember the specific view that had focus
                focusLayoutRestoreView = findFocus();
                if (focusLayoutRestoreView != null) {
                    // Tell it we are going to mess with it
                    focusLayoutRestoreView.onStartTemporaryDetach();
                }
            }

            requestFocus();
        }

        // FIXME: We need a way to save current accessibility focus here
        // so that it can be restored after we re-attach the children on each
        // layout round.

        detachAllViewsFromParent();

        switch (mLayoutMode) {
        case LAYOUT_SET_SELECTION:
            if (newSelected != null) {
                final int newSelectedStart = (mIsVertical ? newSelected.getTop() : newSelected.getLeft());

                selected = fillFromSelection(newSelectedStart, start, end);
            } else {
                selected = fillFromMiddle(start, end);
            }

            break;

        case LAYOUT_SYNC:
            selected = fillSpecific(mSyncPosition, mSpecificStart);
            break;

        case LAYOUT_FORCE_BOTTOM:
            selected = fillBefore(mItemCount - 1, end);
            adjustViewsStartOrEnd();
            break;

        case LAYOUT_FORCE_TOP:
            mFirstPosition = 0;
            selected = fillFromOffset(start);
            adjustViewsStartOrEnd();
            break;

        case LAYOUT_SPECIFIC:
            selected = fillSpecific(reconcileSelectedPosition(), mSpecificStart);
            break;

        case LAYOUT_MOVE_SELECTION:
            selected = moveSelection(oldSelected, newSelected, delta, start, end);
            break;

        default:
            if (childCount == 0) {
                final int position = lookForSelectablePosition(0);
                setSelectedPositionInt(position);
                selected = fillFromOffset(start);
            } else {
                if (mSelectedPosition >= 0 && mSelectedPosition < mItemCount) {
                    int offset = start;
                    if (oldSelected != null) {
                        offset = (mIsVertical ? oldSelected.getTop() : oldSelected.getLeft());
                    }
                    selected = fillSpecific(mSelectedPosition, offset);
                } else if (mFirstPosition < mItemCount) {
                    int offset = start;
                    if (oldFirstChild != null) {
                        offset = (mIsVertical ? oldFirstChild.getTop() : oldFirstChild.getLeft());
                    }

                    selected = fillSpecific(mFirstPosition, offset);
                } else {
                    selected = fillSpecific(0, start);
                }
            }

            break;

        }

        recycleBin.scrapActiveViews();

        if (selected != null) {
            if (mItemsCanFocus && hasFocus() && !selected.hasFocus()) {
                final boolean focusWasTaken = (selected == focusLayoutRestoreDirectChild
                        && focusLayoutRestoreView != null && focusLayoutRestoreView.requestFocus())
                        || selected.requestFocus();

                if (!focusWasTaken) {
                    // Selected item didn't take focus, fine, but still want
                    // to make sure something else outside of the selected view
                    // has focus
                    final View focused = getFocusedChild();
                    if (focused != null) {
                        focused.clearFocus();
                    }

                    positionSelector(INVALID_POSITION, selected);
                } else {
                    selected.setSelected(false);
                    mSelectorRect.setEmpty();
                }
            } else {
                positionSelector(INVALID_POSITION, selected);
            }

            mSelectedStart = (mIsVertical ? selected.getTop() : selected.getLeft());
        } else {
            if (mTouchMode > TOUCH_MODE_DOWN && mTouchMode < TOUCH_MODE_DRAGGING) {
                View child = getChildAt(mMotionPosition - mFirstPosition);

                if (child != null) {
                    positionSelector(mMotionPosition, child);
                }
            } else {
                mSelectedStart = 0;
                mSelectorRect.setEmpty();
            }

            // Even if there is not selected position, we may need to restore
            // focus (i.e. something focusable in touch mode)
            if (hasFocus() && focusLayoutRestoreView != null) {
                focusLayoutRestoreView.requestFocus();
            }
        }

        // Tell focus view we are done mucking with it, if it is still in
        // our view hierarchy.
        if (focusLayoutRestoreView != null && focusLayoutRestoreView.getWindowToken() != null) {
            focusLayoutRestoreView.onFinishTemporaryDetach();
        }

        mLayoutMode = LAYOUT_NORMAL;
        mDataChanged = false;
        mNeedSync = false;

        setNextSelectedPositionInt(mSelectedPosition);
        if (mItemCount > 0) {
            checkSelectionChanged();
        }

        invokeOnItemScrollListener();
    } finally {
        if (!blockLayoutRequests) {
            mBlockLayoutRequests = false;
            mDataChanged = false;
        }
    }
}

From source file:com.aliasapps.seq.scroller.TwoWayView.java

private void layoutChildren() {
    if (getWidth() == 0 || getHeight() == 0) {
        return;/*from  w  w w . j a  v a  2  s. c om*/
    }

    final boolean blockLayoutRequests = mBlockLayoutRequests;
    if (!blockLayoutRequests) {
        mBlockLayoutRequests = true;
    } else {
        return;
    }

    try {
        invalidate();

        if (mAdapter == null) {
            resetState();
            return;
        }

        final int start = (mIsVertical ? getPaddingTop() : getPaddingLeft());
        final int end = (mIsVertical ? getHeight() - getPaddingBottom() : getWidth() - getPaddingRight());

        int childCount = getChildCount();
        int index = 0;
        int delta = 0;

        View focusLayoutRestoreView = null;

        View selected = null;
        View oldSelected = null;
        View newSelected = null;
        View oldFirstChild = null;

        switch (mLayoutMode) {
        case LAYOUT_SET_SELECTION:
            index = mNextSelectedPosition - mFirstPosition;
            if (index >= 0 && index < childCount) {
                newSelected = getChildAt(index);
            }

            break;

        case LAYOUT_FORCE_TOP:
        case LAYOUT_FORCE_BOTTOM:
        case LAYOUT_SPECIFIC:
        case LAYOUT_SYNC:
            break;

        case LAYOUT_MOVE_SELECTION:
        default:
            // Remember the previously selected view
            index = mSelectedPosition - mFirstPosition;
            if (index >= 0 && index < childCount) {
                oldSelected = getChildAt(index);
            }

            // Remember the previous first child
            oldFirstChild = getChildAt(0);

            if (mNextSelectedPosition >= 0) {
                delta = mNextSelectedPosition - mSelectedPosition;
            }

            // Caution: newSelected might be null
            newSelected = getChildAt(index + delta);
        }

        final boolean dataChanged = mDataChanged;
        if (dataChanged) {
            handleDataChanged();
        }

        // Handle the empty set by removing all views that are visible
        // and calling it a day
        if (mItemCount == 0) {
            resetState();
            return;
        } else if (mItemCount != mAdapter.getCount()) {
            throw new IllegalStateException("The content of the adapter has changed but "
                    + "TwoWayView did not receive a notification. Make sure the content of "
                    + "your adapter is not modified from a background thread, but only "
                    + "from the UI thread. [in TwoWayView(" + getId() + ", " + getClass() + ") with Adapter("
                    + mAdapter.getClass() + ")]");
        }

        setSelectedPositionInt(mNextSelectedPosition);

        // Reset the focus restoration
        View focusLayoutRestoreDirectChild = null;

        // Pull all children into the RecycleBin.
        // These views will be reused if possible
        final int firstPosition = mFirstPosition;
        final RecycleBin recycleBin = mRecycler;

        if (dataChanged) {
            for (int i = 0; i < childCount; i++) {
                recycleBin.addScrapView(getChildAt(i), firstPosition + i);
            }
        } else {
            recycleBin.fillActiveViews(childCount, firstPosition);
        }

        // Take focus back to us temporarily to avoid the eventual
        // call to clear focus when removing the focused child below
        // from messing things up when ViewAncestor assigns focus back
        // to someone else.
        final View focusedChild = getFocusedChild();
        if (focusedChild != null) {
            // We can remember the focused view to restore after relayout if the
            // data hasn't changed, or if the focused position is a header or footer.
            if (!dataChanged) {
                focusLayoutRestoreDirectChild = focusedChild;

                // Remember the specific view that had focus
                focusLayoutRestoreView = findFocus();
                if (focusLayoutRestoreView != null) {
                    // Tell it we are going to mess with it
                    focusLayoutRestoreView.onStartTemporaryDetach();
                }
            }

            requestFocus();
        }

        // FIXME: We need a way to save current accessibility focus here
        // so that it can be restored after we re-attach the children on each
        // layout round.

        detachAllViewsFromParent();

        switch (mLayoutMode) {
        case LAYOUT_SET_SELECTION:
            if (newSelected != null) {
                final int newSelectedStart = (mIsVertical ? newSelected.getTop() : newSelected.getLeft());

                selected = fillFromSelection(newSelectedStart, start, end);
            } else {
                selected = fillFromMiddle(start, end);
            }

            break;

        case LAYOUT_SYNC:
            selected = fillSpecific(mSyncPosition, mSpecificStart);
            break;

        case LAYOUT_FORCE_BOTTOM:
            selected = fillBefore(mItemCount - 1, end);
            adjustViewsStartOrEnd();
            break;

        case LAYOUT_FORCE_TOP:
            mFirstPosition = 0;
            selected = fillFromOffset(start);
            adjustViewsStartOrEnd();
            break;

        case LAYOUT_SPECIFIC:
            selected = fillSpecific(reconcileSelectedPosition(), mSpecificStart);
            break;

        case LAYOUT_MOVE_SELECTION:
            selected = moveSelection(oldSelected, newSelected, delta, start, end);
            break;

        default:
            if (childCount == 0) {
                final int position = lookForSelectablePosition(0);
                setSelectedPositionInt(position);
                selected = fillFromOffset(start);
            } else {
                if (mSelectedPosition >= 0 && mSelectedPosition < mItemCount) {
                    int offset = start;
                    if (oldSelected != null) {
                        offset = (mIsVertical ? oldSelected.getTop() : oldSelected.getLeft());
                    }
                    selected = fillSpecific(mSelectedPosition, offset);
                } else if (mFirstPosition < mItemCount) {
                    int offset = start;
                    if (oldFirstChild != null) {
                        offset = (mIsVertical ? oldFirstChild.getTop() : oldFirstChild.getLeft());
                    }

                    selected = fillSpecific(mFirstPosition, offset);
                } else {
                    selected = fillSpecific(0, start);
                }
            }

            break;

        }

        recycleBin.scrapActiveViews();

        if (selected != null) {
            if (mItemsCanFocus && hasFocus() && !selected.hasFocus()) {
                final boolean focusWasTaken = (selected == focusLayoutRestoreDirectChild
                        && focusLayoutRestoreView != null && focusLayoutRestoreView.requestFocus())
                        || selected.requestFocus();

                if (!focusWasTaken) {
                    // Selected item didn't take focus, fine, but still want
                    // to make sure something else outside of the selected view
                    // has focus
                    final View focused = getFocusedChild();
                    if (focused != null) {
                        focused.clearFocus();
                    }

                    positionSelector(INVALID_POSITION, selected);
                } else {
                    selected.setSelected(false);
                    mSelectorRect.setEmpty();
                }
            } else {
                positionSelector(INVALID_POSITION, selected);
            }

            mSelectedStart = (mIsVertical ? selected.getTop() : selected.getLeft());
        } else {
            if (mTouchMode > TOUCH_MODE_DOWN && mTouchMode < TOUCH_MODE_DRAGGING) {
                View child = getChildAt(mMotionPosition - mFirstPosition);

                if (child != null) {
                    positionSelector(mMotionPosition, child);
                }
            } else {
                mSelectedStart = 0;
                mSelectorRect.setEmpty();
            }

            // Even if there is not selected position, we may need to restore
            // focus (i.e. something focusable in touch mode)
            if (hasFocus() && focusLayoutRestoreView != null) {
                focusLayoutRestoreView.requestFocus();
            }
        }

        // Tell focus view we are done mucking with it, if it is still in
        // our view hierarchy.
        if (focusLayoutRestoreView != null && focusLayoutRestoreView.getWindowToken() != null) {
            focusLayoutRestoreView.onFinishTemporaryDetach();
        }

        mLayoutMode = LAYOUT_NORMAL;
        mDataChanged = false;
        mNeedSync = false;

        setNextSelectedPositionInt(mSelectedPosition);
        if (mItemCount > 0) {
            checkSelectionChanged();
        }

        invokeOnItemScrollListener();
    } finally {
        if (!blockLayoutRequests) {
            mBlockLayoutRequests = false;
            mDataChanged = false;
        }
    }
}

From source file:com.artifex.mupdf.view.ThumbnailViews.java

private void layoutChildren() {
    if (getWidth() == 0 || getHeight() == 0) {
        return;//from w  ww  .jav  a2 s .  c  o  m
    }

    final boolean blockLayoutRequests = mBlockLayoutRequests;
    if (!blockLayoutRequests) {
        mBlockLayoutRequests = true;
    } else {
        return;
    }

    try {
        invalidate();

        if (mAdapter == null) {
            resetState();
            return;
        }

        final int start = (mIsVertical ? getPaddingTop() : getPaddingLeft());
        final int end = (mIsVertical ? getHeight() - getPaddingBottom() : getWidth() - getPaddingRight());

        int childCount = getChildCount();
        int index = 0;
        int delta = 0;

        View focusLayoutRestoreView = null;

        View selected = null;
        View oldSelected = null;
        View newSelected = null;
        View oldFirstChild = null;

        switch (mLayoutMode) {
        case LAYOUT_SET_SELECTION:
            index = mNextSelectedPosition - mFirstPosition;
            if (index >= 0 && index < childCount) {
                newSelected = getChildAt(index);
            }

            break;

        case LAYOUT_FORCE_TOP:
        case LAYOUT_FORCE_BOTTOM:
        case LAYOUT_SPECIFIC:
        case LAYOUT_SYNC:
            break;

        case LAYOUT_MOVE_SELECTION:
        default:
            // Remember the previously selected view
            index = mSelectedPosition - mFirstPosition;
            if (index >= 0 && index < childCount) {
                oldSelected = getChildAt(index);
            }

            // Remember the previous first child
            oldFirstChild = getChildAt(0);

            if (mNextSelectedPosition >= 0) {
                delta = mNextSelectedPosition - mSelectedPosition;
            }

            // Caution: newSelected might be null
            newSelected = getChildAt(index + delta);
        }

        final boolean dataChanged = mDataChanged;
        if (dataChanged) {
            handleDataChanged();
        }

        // Handle the empty set by removing all views that are visible
        // and calling it a day
        if (mItemCount == 0) {
            resetState();
            return;
        } else if (mItemCount != mAdapter.getCount()) {
            throw new IllegalStateException("The content of the adapter has changed but "
                    + "TwoWayView did not receive a notification. Make sure the content of "
                    + "your adapter is not modified from a background thread, but only "
                    + "from the UI thread. [in TwoWayView(" + getId() + ", " + getClass() + ") with Adapter("
                    + mAdapter.getClass() + ")]");
        }

        setSelectedPositionInt(mNextSelectedPosition);

        // Reset the focus restoration
        View focusLayoutRestoreDirectChild = null;

        // Pull all children into the RecycleBin.
        // These views will be reused if possible
        final int firstPosition = mFirstPosition;
        final RecycleBin recycleBin = mRecycler;

        if (dataChanged) {
            for (int i = 0; i < childCount; i++) {
                recycleBin.addScrapView(getChildAt(i), firstPosition + i);
            }
        } else {
            recycleBin.fillActiveViews(childCount, firstPosition);
        }

        // Take focus back to us temporarily to avoid the eventual
        // call to clear focus when removing the focused child below
        // from messing things up when ViewAncestor assigns focus back
        // to someone else.
        final View focusedChild = getFocusedChild();
        if (focusedChild != null) {
            // We can remember the focused view to restore after relayout if
            // the
            // data hasn't changed, or if the focused position is a header
            // or footer.
            if (!dataChanged) {
                focusLayoutRestoreDirectChild = focusedChild;

                // Remember the specific view that had focus
                focusLayoutRestoreView = findFocus();
                if (focusLayoutRestoreView != null) {
                    // Tell it we are going to mess with it
                    focusLayoutRestoreView.onStartTemporaryDetach();
                }
            }

            requestFocus();
        }

        // FIXME: We need a way to save current accessibility focus here
        // so that it can be restored after we re-attach the children on
        // each
        // layout round.

        detachAllViewsFromParent();

        switch (mLayoutMode) {
        case LAYOUT_SET_SELECTION:
            if (newSelected != null) {
                final int newSelectedStart = (mIsVertical ? newSelected.getTop() : newSelected.getLeft());

                selected = fillFromSelection(newSelectedStart, start, end);
            } else {
                selected = fillFromMiddle(start, end);
            }

            break;

        case LAYOUT_SYNC:
            selected = fillSpecific(mSyncPosition, mSpecificStart);
            break;

        case LAYOUT_FORCE_BOTTOM:
            selected = fillBefore(mItemCount - 1, end);
            adjustViewsStartOrEnd();
            break;

        case LAYOUT_FORCE_TOP:
            mFirstPosition = 0;
            selected = fillFromOffset(start);
            adjustViewsStartOrEnd();
            break;

        case LAYOUT_SPECIFIC:
            selected = fillSpecific(reconcileSelectedPosition(), mSpecificStart);
            break;

        case LAYOUT_MOVE_SELECTION:
            selected = moveSelection(oldSelected, newSelected, delta, start, end);
            break;

        default:
            if (childCount == 0) {
                final int position = lookForSelectablePosition(0);
                setSelectedPositionInt(position);
                selected = fillFromOffset(start);
            } else {
                if (mSelectedPosition >= 0 && mSelectedPosition < mItemCount) {
                    int offset = start;
                    if (oldSelected != null) {
                        offset = (mIsVertical ? oldSelected.getTop() : oldSelected.getLeft());
                    }
                    selected = fillSpecific(mSelectedPosition, offset);
                } else if (mFirstPosition < mItemCount) {
                    int offset = start;
                    if (oldFirstChild != null) {
                        offset = (mIsVertical ? oldFirstChild.getTop() : oldFirstChild.getLeft());
                    }

                    selected = fillSpecific(mFirstPosition, offset);
                } else {
                    selected = fillSpecific(0, start);
                }
            }

            break;

        }

        recycleBin.scrapActiveViews();

        if (selected != null) {
            if (mItemsCanFocus && hasFocus() && !selected.hasFocus()) {
                final boolean focusWasTaken = (selected == focusLayoutRestoreDirectChild
                        && focusLayoutRestoreView != null && focusLayoutRestoreView.requestFocus())
                        || selected.requestFocus();

                if (!focusWasTaken) {
                    // Selected item didn't take focus, fine, but still want
                    // to make sure something else outside of the selected
                    // view
                    // has focus
                    final View focused = getFocusedChild();
                    if (focused != null) {
                        focused.clearFocus();
                    }

                    positionSelector(INVALID_POSITION, selected);
                } else {
                    selected.setSelected(false);
                    mSelectorRect.setEmpty();
                }
            } else {
                positionSelector(INVALID_POSITION, selected);
            }

            mSelectedStart = (mIsVertical ? selected.getTop() : selected.getLeft());
        } else {
            if (mTouchMode > TOUCH_MODE_DOWN && mTouchMode < TOUCH_MODE_DRAGGING) {
                View child = getChildAt(mMotionPosition - mFirstPosition);

                if (child != null) {
                    positionSelector(mMotionPosition, child);
                }
            } else {
                mSelectedStart = 0;
                mSelectorRect.setEmpty();
            }

            // Even if there is not selected position, we may need to
            // restore
            // focus (i.e. something focusable in touch mode)
            if (hasFocus() && focusLayoutRestoreView != null) {
                focusLayoutRestoreView.requestFocus();
            }
        }

        // Tell focus view we are done mucking with it, if it is still in
        // our view hierarchy.
        if (focusLayoutRestoreView != null && focusLayoutRestoreView.getWindowToken() != null) {
            focusLayoutRestoreView.onFinishTemporaryDetach();
        }

        mLayoutMode = LAYOUT_NORMAL;
        mDataChanged = false;
        mNeedSync = false;

        setNextSelectedPositionInt(mSelectedPosition);
        if (mItemCount > 0) {
            checkSelectionChanged();
        }

        invokeOnItemScrollListener();
    } finally {
        if (!blockLayoutRequests) {
            mBlockLayoutRequests = false;
            mDataChanged = false;
        }
    }
}

From source file:android.support.v71.widget.RecyclerView.java

private void repositionShadowingViews() {
    // Fix up shadow views used by change animations
    int count = mChildHelper.getChildCount();
    for (int i = 0; i < count; i++) {
        View view = mChildHelper.getChildAt(i);
        ViewHolder holder = getChildViewHolder(view);
        if (holder != null && holder.mShadowingHolder != null) {
            View shadowingView = holder.mShadowingHolder.itemView;
            int left = view.getLeft();
            int top = view.getTop();
            if (left != shadowingView.getLeft() || top != shadowingView.getTop()) {
                shadowingView.layout(left, top, left + shadowingView.getWidth(),
                        top + shadowingView.getHeight());
            }//w  ww.j av  a  2s .c om
        }
    }
}