Example usage for android.view View requestFocus

List of usage examples for android.view View requestFocus

Introduction

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

Prototype

public final boolean requestFocus(int direction) 

Source Link

Document

Call this to try to give focus to a specific view or to one of its descendants and give it a hint about what direction focus is heading.

Usage

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

/**
 * Do an arrow scroll based on focus searching.  If a new view is
 * given focus, return the selection delta and amount to scroll via
 * an {@link ArrowScrollFocusResult}, otherwise, return null.
 *
 * @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./*from   w  w  w.ja va  2s.c om*/
 *
 * @return The result if focus has changed, or <code>null</code>.
 */
private ArrowScrollFocusResult arrowScrollFocused(final int direction) {
    forceValidFocusDirection(direction);

    final View selectedView = getSelectedView();
    final View newFocus;
    final int searchPoint;

    if (selectedView != null && selectedView.hasFocus()) {
        View oldFocus = selectedView.findFocus();
        newFocus = FocusFinder.getInstance().findNextFocus(this, oldFocus, direction);
    } else {
        if (direction == View.FOCUS_DOWN || direction == View.FOCUS_RIGHT) {
            final int start = (mIsVertical ? getPaddingTop() : getPaddingLeft());

            final int selectedStart;
            if (selectedView != null) {
                selectedStart = (mIsVertical ? selectedView.getTop() : selectedView.getLeft());
            } else {
                selectedStart = start;
            }

            searchPoint = Math.max(selectedStart, start);
        } else {
            final int end = (mIsVertical ? getHeight() - getPaddingBottom() : getWidth() - getPaddingRight());

            final int selectedEnd;
            if (selectedView != null) {
                selectedEnd = (mIsVertical ? selectedView.getBottom() : selectedView.getRight());
            } else {
                selectedEnd = end;
            }

            searchPoint = Math.min(selectedEnd, end);
        }

        final int x = (mIsVertical ? 0 : searchPoint);
        final int y = (mIsVertical ? searchPoint : 0);
        mTempRect.set(x, y, x, y);

        newFocus = FocusFinder.getInstance().findNextFocusFromRect(this, mTempRect, direction);
    }

    if (newFocus != null) {
        final int positionOfNewFocus = positionOfNewFocus(newFocus);

        // If the focus change is in a different new position, make sure
        // we aren't jumping over another selectable position.
        if (mSelectedPosition != INVALID_POSITION && positionOfNewFocus != mSelectedPosition) {
            final int selectablePosition = lookForSelectablePositionOnScreen(direction);

            final boolean movingForward = (direction == View.FOCUS_DOWN || direction == View.FOCUS_RIGHT);
            final boolean movingBackward = (direction == View.FOCUS_UP || direction == View.FOCUS_LEFT);

            if (selectablePosition != INVALID_POSITION
                    && ((movingForward && selectablePosition < positionOfNewFocus)
                            || (movingBackward && selectablePosition > positionOfNewFocus))) {
                return null;
            }
        }

        int focusScroll = amountToScrollToNewFocus(direction, newFocus, positionOfNewFocus);

        final int maxScrollAmount = getMaxScrollAmount();
        if (focusScroll < maxScrollAmount) {
            // Not moving too far, safe to give next view focus
            newFocus.requestFocus(direction);
            mArrowScrollFocusResult.populate(positionOfNewFocus, focusScroll);
            return mArrowScrollFocusResult;
        } else if (distanceToView(newFocus) < maxScrollAmount) {
            // Case to consider:
            // Too far to get entire next focusable on screen, but by going
            // max scroll amount, we are getting it at least partially in view,
            // so give it focus and scroll the max amount.
            newFocus.requestFocus(direction);
            mArrowScrollFocusResult.populate(positionOfNewFocus, maxScrollAmount);
            return mArrowScrollFocusResult;
        }
    }

    return null;
}

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

/**
 * Do an arrow scroll based on focus searching. If a new view is given
 * focus, return the selection delta and amount to scroll via an
 * {@link ArrowScrollFocusResult}, otherwise, return null.
 * /*from  ww w.  j av a  2 s  . co 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 The result if focus has changed, or <code>null</code>.
 */
private ArrowScrollFocusResult arrowScrollFocused(final int direction) {
    forceValidFocusDirection(direction);

    final View selectedView = getSelectedView();
    final View newFocus;
    final int searchPoint;

    if (selectedView != null && selectedView.hasFocus()) {
        View oldFocus = selectedView.findFocus();
        newFocus = FocusFinder.getInstance().findNextFocus(this, oldFocus, direction);
    } else {
        if (direction == View.FOCUS_DOWN || direction == View.FOCUS_RIGHT) {
            final int start = (mIsVertical ? getPaddingTop() : getPaddingLeft());

            final int selectedStart;
            if (selectedView != null) {
                selectedStart = (mIsVertical ? selectedView.getTop() : selectedView.getLeft());
            } else {
                selectedStart = start;
            }

            searchPoint = Math.max(selectedStart, start);
        } else {
            final int end = (mIsVertical ? getHeight() - getPaddingBottom() : getWidth() - getPaddingRight());

            final int selectedEnd;
            if (selectedView != null) {
                selectedEnd = (mIsVertical ? selectedView.getBottom() : selectedView.getRight());
            } else {
                selectedEnd = end;
            }

            searchPoint = Math.min(selectedEnd, end);
        }

        final int x = (mIsVertical ? 0 : searchPoint);
        final int y = (mIsVertical ? searchPoint : 0);
        mTempRect.set(x, y, x, y);

        newFocus = FocusFinder.getInstance().findNextFocusFromRect(this, mTempRect, direction);
    }

    if (newFocus != null) {
        final int positionOfNewFocus = positionOfNewFocus(newFocus);

        // If the focus change is in a different new position, make sure
        // we aren't jumping over another selectable position.
        if (mSelectedPosition != INVALID_POSITION && positionOfNewFocus != mSelectedPosition) {
            final int selectablePosition = lookForSelectablePositionOnScreen(direction);

            final boolean movingForward = (direction == View.FOCUS_DOWN || direction == View.FOCUS_RIGHT);
            final boolean movingBackward = (direction == View.FOCUS_UP || direction == View.FOCUS_LEFT);

            if (selectablePosition != INVALID_POSITION
                    && ((movingForward && selectablePosition < positionOfNewFocus)
                            || (movingBackward && selectablePosition > positionOfNewFocus))) {
                return null;
            }
        }

        int focusScroll = amountToScrollToNewFocus(direction, newFocus, positionOfNewFocus);

        final int maxScrollAmount = getMaxScrollAmount();
        if (focusScroll < maxScrollAmount) {
            // Not moving too far, safe to give next view focus
            newFocus.requestFocus(direction);
            mArrowScrollFocusResult.populate(positionOfNewFocus, focusScroll);
            return mArrowScrollFocusResult;
        } else if (distanceToView(newFocus) < maxScrollAmount) {
            // Case to consider:
            // Too far to get entire next focusable on screen, but by going
            // max scroll amount, we are getting it at least partially in
            // view,
            // so give it focus and scroll the max amount.
            newFocus.requestFocus(direction);
            mArrowScrollFocusResult.populate(positionOfNewFocus, maxScrollAmount);
            return mArrowScrollFocusResult;
        }
    }

    return null;
}

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

/**
 * Do an arrow scroll based on focus searching.  If a new view is
 * given focus, return the selection delta and amount to scroll via
 * an {@link ArrowScrollFocusResult}, otherwise, return null.
 *
 * @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.//from   ww  w . j  av  a2  s . co m
 *
 * @return The result if focus has changed, or <code>null</code>.
 */
private ArrowScrollFocusResult arrowScrollFocused(final int direction) {
    forceValidFocusDirection(direction);

    final View selectedView = getSelectedView();
    final View newFocus;
    final int searchPoint;

    if (selectedView != null && selectedView.hasFocus()) {
        View oldFocus = selectedView.findFocus();
        newFocus = FocusFinder.getInstance().findNextFocus(this, oldFocus, direction);
    } else {
        if (direction == View.FOCUS_DOWN || direction == View.FOCUS_RIGHT) {
            boolean fadingEdgeShowing = (mFirstPosition > 0);
            final int start = getStartEdge() + (fadingEdgeShowing ? getArrowScrollPreviewLength() : 0);

            final int selectedStart;
            if (selectedView != null) {
                selectedStart = getChildStartEdge(selectedView);
            } else {
                selectedStart = start;
            }

            searchPoint = Math.max(selectedStart, start);
        } else {
            final boolean fadingEdgeShowing = (mFirstPosition + getChildCount() - 1) < mItemCount;
            final int end = getEndEdge() - (fadingEdgeShowing ? getArrowScrollPreviewLength() : 0);

            final int selectedEnd;
            if (selectedView != null) {
                selectedEnd = getChildEndEdge(selectedView);
            } else {
                selectedEnd = end;
            }

            searchPoint = Math.min(selectedEnd, end);
        }

        final int x = (mIsVertical ? 0 : searchPoint);
        final int y = (mIsVertical ? searchPoint : 0);
        mTempRect.set(x, y, x, y);

        newFocus = FocusFinder.getInstance().findNextFocusFromRect(this, mTempRect, direction);
    }

    if (newFocus != null) {
        final int positionOfNewFocus = positionOfNewFocus(newFocus);

        // If the focus change is in a different new position, make sure
        // we aren't jumping over another selectable position.
        if (mSelectedPosition != INVALID_POSITION && positionOfNewFocus != mSelectedPosition) {
            final int selectablePosition = lookForSelectablePositionOnScreen(direction);

            final boolean movingForward = (direction == View.FOCUS_DOWN || direction == View.FOCUS_RIGHT);
            final boolean movingBackward = (direction == View.FOCUS_UP || direction == View.FOCUS_LEFT);

            if (selectablePosition != INVALID_POSITION
                    && ((movingForward && selectablePosition < positionOfNewFocus)
                            || (movingBackward && selectablePosition > positionOfNewFocus))) {
                return null;
            }
        }

        int focusScroll = amountToScrollToNewFocus(direction, newFocus, positionOfNewFocus);

        final int maxScrollAmount = getMaxScrollAmount();
        if (focusScroll < maxScrollAmount) {
            // Not moving too far, safe to give next view focus
            newFocus.requestFocus(direction);
            mArrowScrollFocusResult.populate(positionOfNewFocus, focusScroll);
            return mArrowScrollFocusResult;
        } else if (distanceToView(newFocus) < maxScrollAmount) {
            // Case to consider:
            // Too far to get entire next focusable on screen, but by going
            // max scroll amount, we are getting it at least partially in view,
            // so give it focus and scroll the max amount.
            newFocus.requestFocus(direction);
            mArrowScrollFocusResult.populate(positionOfNewFocus, maxScrollAmount);
            return mArrowScrollFocusResult;
        }
    }

    return null;
}