Java Code Examples for android.view.View#FOCUS_DOWN

The following examples show how to use android.view.View#FOCUS_DOWN . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: MyBlogDemo   File: NestedScrollView.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * <p>Handles scrolling in response to a "home/end" shortcut press. This
 * method will scroll the view to the top or bottom and give the focus
 * to the topmost/bottommost component in the new visible area. If no
 * component is a good candidate for focus, this scrollview reclaims the
 * focus.</p>
 *
 * @param direction the scroll direction: {@link android.view.View#FOCUS_UP}
 *                  to go the top of the view or
 *                  {@link android.view.View#FOCUS_DOWN} to go the bottom
 * @return true if the key event is consumed by this method, false otherwise
 */
public boolean fullScroll(int direction) {
    boolean down = direction == View.FOCUS_DOWN;
    int height = getHeight();

    mTempRect.top = 0;
    mTempRect.bottom = height;

    if (down) {
        int count = getChildCount();
        if (count > 0) {
            View view = getChildAt(count - 1);
            mTempRect.bottom = view.getBottom() + getPaddingBottom();
            mTempRect.top = mTempRect.bottom - height;
        }
    }

    return scrollAndFocus(direction, mTempRect.top, mTempRect.bottom);
}
 
Example 2
/**
 * <p>Handles scrolling in response to a "page up/down" shortcut press. This
 * method will scroll the view by one page up or down and give the focus
 * to the topmost/bottommost component in the new visible area. If no
 * component is a good candidate for focus, this scrollview reclaims the
 * focus.</p>
 *
 * @param direction the scroll direction: {@link View#FOCUS_UP}
 *                  to go one page up or
 *                  {@link View#FOCUS_DOWN} to go one page down
 * @return true if the key event is consumed by this method, false otherwise
 */
public boolean pageScroll(int direction) {
    boolean down = direction == View.FOCUS_DOWN;
    int height = getHeight();

    if (down) {
        mTempRect.top = getScrollY() + height;
        int count = getChildCount();
        if (count > 0) {
            View view = getChildAt(count - 1);
            if (mTempRect.top + height > view.getBottom()) {
                mTempRect.top = view.getBottom() - height;
            }
        }
    } else {
        mTempRect.top = getScrollY() - height;
        if (mTempRect.top < 0) {
            mTempRect.top = 0;
        }
    }
    mTempRect.bottom = mTempRect.top + height;

    return scrollAndFocus(direction, mTempRect.top, mTempRect.bottom);
}
 
Example 3
@Override
public View onFocusSearch(View focused, int direction) {
    if (DEBUG) Log.v(TAG, "onFocusSearch focused " + focused + " + direction " + direction);

    final View searchOrbView = mTitleView.getSearchAffordanceView();
    if (focused == searchOrbView && (
            direction == View.FOCUS_DOWN || direction == View.FOCUS_RIGHT)) {
        return mGridViewHolder.view;

    } else if (focused != searchOrbView && searchOrbView.getVisibility() == View.VISIBLE
            && direction == View.FOCUS_UP) {
        return searchOrbView;

    } else {
        return null;
    }
}
 
Example 4
private int getMovement(int direction) {
    if (mOrientation == VERTICAL) {
        if (direction == View.FOCUS_UP) {
            return mShouldReverseLayout ? DIRECTION_FORWARD : DIRECTION_BACKWARD;
        } else if (direction == View.FOCUS_DOWN) {
            return mShouldReverseLayout ? DIRECTION_BACKWARD : DIRECTION_FORWARD;
        } else {
            return DIRECTION_NO_WHERE;
        }
    } else {
        if (direction == View.FOCUS_LEFT) {
            return mShouldReverseLayout ? DIRECTION_FORWARD : DIRECTION_BACKWARD;
        } else if (direction == View.FOCUS_RIGHT) {
            return mShouldReverseLayout ? DIRECTION_BACKWARD : DIRECTION_FORWARD;
        } else {
            return DIRECTION_NO_WHERE;
        }
    }
}
 
Example 5
Source Project: AndroidQuick   File: TwoDScrollView.java    License: MIT License 6 votes vote down vote up
/**
 * When looking for focus in children of a scroll view, need to be a little
 * more careful not to give focus to something that is scrolled off screen.
 * <p/>
 * This is more expensive than the default {@link android.view.ViewGroup}
 * implementation, otherwise this behavior might have been made the default.
 */
@Override
protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
    // convert from forward / backward notation to up / down / left / right
    // (ugh).
    if (direction == View.FOCUS_FORWARD) {
        direction = View.FOCUS_DOWN;
    } else if (direction == View.FOCUS_BACKWARD) {
        direction = View.FOCUS_UP;
    }

    final View nextFocus = previouslyFocusedRect == null ?
            FocusFinder.getInstance().findNextFocus(this, null, direction) :
            FocusFinder.getInstance().findNextFocusFromRect(this,
                    previouslyFocusedRect, direction);

    if (nextFocus == null) {
        return false;
    }

    return nextFocus.requestFocus(direction, previouslyFocusedRect);
}
 
Example 6
/**
 * Converts a focusDirection to orientation.
 *
 * @param focusDirection One of {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
 *                       {@link View#FOCUS_LEFT}, {@link View#FOCUS_RIGHT},
 *                       {@link View#FOCUS_BACKWARD}, {@link View#FOCUS_FORWARD}
 *                       or 0 for not applicable
 * @return {@link RenderState#LAYOUT_START} or {@link RenderState#LAYOUT_END} if focus direction
 * is applicable to current state, {@link RenderState#INVALID_LAYOUT} otherwise.
 */
private int convertFocusDirectionToLayoutDirection(int focusDirection) {
    switch (focusDirection) {
        case View.FOCUS_BACKWARD:
            return RenderState.LAYOUT_START;
        case View.FOCUS_FORWARD:
            return RenderState.LAYOUT_END;
        case View.FOCUS_UP:
            return mOrientation == VERTICAL ? RenderState.LAYOUT_START
                    : RenderState.INVALID_LAYOUT;
        case View.FOCUS_DOWN:
            return mOrientation == VERTICAL ? RenderState.LAYOUT_END
                    : RenderState.INVALID_LAYOUT;
        case View.FOCUS_LEFT:
            return mOrientation == HORIZONTAL ? RenderState.LAYOUT_START
                    : RenderState.INVALID_LAYOUT;
        case View.FOCUS_RIGHT:
            return mOrientation == HORIZONTAL ? RenderState.LAYOUT_END
                    : RenderState.INVALID_LAYOUT;
        default:
            if (DEBUG) {
                Log.d(TAG, "Unknown focus request:" + focusDirection);
            }
            return RenderState.INVALID_LAYOUT;
    }

}
 
Example 7
@Override
protected void onFocusChanged(boolean focused, int direction,
        Rect previouslyFocusedRect) {
    super.onFocusChanged(focused, direction, previouslyFocusedRect);

    if (focused) {
        switch (direction) {
            case View.FOCUS_DOWN:
                mSelectedRow = 0;
                break;
            case View.FOCUS_UP:
                mSelectedRow = mNumRows - 1;
                break;
            case View.FOCUS_LEFT:  // fall through
            case View.FOCUS_RIGHT:
                // set the row that is closest to the rect
                if (previouslyFocusedRect != null) {
                    int y = previouslyFocusedRect.top
                            + (previouslyFocusedRect.height() / 2);
                    int yPerRow = getHeight() / mNumRows;
                    mSelectedRow = y / yPerRow;
                } else {
                    mSelectedRow = 0;
                }
                break;
            default:
                // can't gleam any useful information about what internal
                // selection should be...
                return;
        }
        invalidate();
    }
}
 
Example 8
Source Project: Mupdf   File: LinearLayoutManager.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Converts a focusDirection to orientation.
 *
 * @param focusDirection One of {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
 *                       {@link View#FOCUS_LEFT}, {@link View#FOCUS_RIGHT},
 *                       {@link View#FOCUS_BACKWARD}, {@link View#FOCUS_FORWARD}
 *                       or 0 for not applicable
 * @return {@link LayoutState#LAYOUT_START} or {@link LayoutState#LAYOUT_END} if focus direction
 * is applicable to current state, {@link LayoutState#INVALID_LAYOUT} otherwise.
 */
private int convertFocusDirectionToLayoutDirection(int focusDirection) {
    switch (focusDirection) {
        case View.FOCUS_BACKWARD:
            return LayoutState.LAYOUT_START;
        case View.FOCUS_FORWARD:
            return LayoutState.LAYOUT_END;
        case View.FOCUS_UP:
            return mOrientation == VERTICAL ? LayoutState.LAYOUT_START
                    : LayoutState.INVALID_LAYOUT;
        case View.FOCUS_DOWN:
            return mOrientation == VERTICAL ? LayoutState.LAYOUT_END
                    : LayoutState.INVALID_LAYOUT;
        case View.FOCUS_LEFT:
            return mOrientation == HORIZONTAL ? LayoutState.LAYOUT_START
                    : LayoutState.INVALID_LAYOUT;
        case View.FOCUS_RIGHT:
            return mOrientation == HORIZONTAL ? LayoutState.LAYOUT_END
                    : LayoutState.INVALID_LAYOUT;
        default:
            if (DEBUG) {
                Log.d(TAG, "Unknown focus request:" + focusDirection);
            }
            return LayoutState.INVALID_LAYOUT;
    }

}
 
Example 9
/**
 * Converts a focusDirection to orientation.
 *
 * @param focusDirection One of {@link android.view.View#FOCUS_UP}, {@link android.view.View#FOCUS_DOWN},
 *                       {@link android.view.View#FOCUS_LEFT}, {@link android.view.View#FOCUS_RIGHT},
 *                       {@link android.view.View#FOCUS_BACKWARD}, {@link android.view.View#FOCUS_FORWARD}
 *                       or 0 for not applicable
 * @return {@link RenderState#LAYOUT_START} or {@link RenderState#LAYOUT_END} if focus direction
 * is applicable to current state, {@link RenderState#INVALID_LAYOUT} otherwise.
 */
private int convertFocusDirectionToLayoutDirection(int focusDirection) {
    switch (focusDirection) {
        case View.FOCUS_BACKWARD:
            return RenderState.LAYOUT_START;
        case View.FOCUS_FORWARD:
            return RenderState.LAYOUT_END;
        case View.FOCUS_UP:
            return mOrientation == VERTICAL ? RenderState.LAYOUT_START
                    : RenderState.INVALID_LAYOUT;
        case View.FOCUS_DOWN:
            return mOrientation == VERTICAL ? RenderState.LAYOUT_END
                    : RenderState.INVALID_LAYOUT;
        case View.FOCUS_LEFT:
            return mOrientation == HORIZONTAL ? RenderState.LAYOUT_START
                    : RenderState.INVALID_LAYOUT;
        case View.FOCUS_RIGHT:
            return mOrientation == HORIZONTAL ? RenderState.LAYOUT_END
                    : RenderState.INVALID_LAYOUT;
        default:
            if (DEBUG) {
                Log.d(TAG, "Unknown focus request:" + focusDirection);
            }
            return RenderState.INVALID_LAYOUT;
    }

}
 
Example 10
/**
 * Converts a focusDirection to orientation.
 *
 * @param focusDirection One of {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
 *                       {@link View#FOCUS_LEFT}, {@link View#FOCUS_RIGHT},
 *                       {@link View#FOCUS_BACKWARD}, {@link View#FOCUS_FORWARD}
 *                       or 0 for not applicable
 * @return {@link LayoutState#LAYOUT_START} or {@link LayoutState#LAYOUT_END} if focus direction
 * is applicable to current state, {@link LayoutState#INVALID_LAYOUT} otherwise.
 */
int convertFocusDirectionToLayoutDirection(int focusDirection) {
    switch (focusDirection) {
        case View.FOCUS_BACKWARD:
            if (mOrientation == VERTICAL) {
                return LayoutState.LAYOUT_START;
            } else if (isLayoutRTL()) {
                return LayoutState.LAYOUT_END;
            } else {
                return LayoutState.LAYOUT_START;
            }
        case View.FOCUS_FORWARD:
            if (mOrientation == VERTICAL) {
                return LayoutState.LAYOUT_END;
            } else if (isLayoutRTL()) {
                return LayoutState.LAYOUT_START;
            } else {
                return LayoutState.LAYOUT_END;
            }
        case View.FOCUS_UP:
            return mOrientation == VERTICAL ? LayoutState.LAYOUT_START
                    : LayoutState.INVALID_LAYOUT;
        case View.FOCUS_DOWN:
            return mOrientation == VERTICAL ? LayoutState.LAYOUT_END
                    : LayoutState.INVALID_LAYOUT;
        case View.FOCUS_LEFT:
            return mOrientation == HORIZONTAL ? LayoutState.LAYOUT_START
                    : LayoutState.INVALID_LAYOUT;
        case View.FOCUS_RIGHT:
            return mOrientation == HORIZONTAL ? LayoutState.LAYOUT_END
                    : LayoutState.INVALID_LAYOUT;
        default:
            if (DEBUG) {
                Log.d(TAG, "Unknown focus request:" + focusDirection);
            }
            return LayoutState.INVALID_LAYOUT;
    }

}
 
Example 11
/**
 * Converts a focusDirection to orientation.
 *
 * @param focusDirection One of {@link android.view.View#FOCUS_UP}, {@link android.view.View#FOCUS_DOWN},
 *                       {@link android.view.View#FOCUS_LEFT}, {@link android.view.View#FOCUS_RIGHT},
 *                       {@link android.view.View#FOCUS_BACKWARD}, {@link android.view.View#FOCUS_FORWARD}
 *                       or 0 for not applicable
 * @return {@link com.twotoasters.android.support.v7.widget.LinearLayoutManager.RenderState#LAYOUT_START} or {@link com.twotoasters.android.support.v7.widget.LinearLayoutManager.RenderState#LAYOUT_END} if focus direction
 * is applicable to current state, {@link com.twotoasters.android.support.v7.widget.LinearLayoutManager.RenderState#INVALID_LAYOUT} otherwise.
 */
private int convertFocusDirectionToLayoutDirection(int focusDirection) {
    switch (focusDirection) {
        case View.FOCUS_BACKWARD:
            return RenderState.LAYOUT_START;
        case View.FOCUS_FORWARD:
            return RenderState.LAYOUT_END;
        case View.FOCUS_UP:
            return mOrientation == VERTICAL ? RenderState.LAYOUT_START
                    : RenderState.INVALID_LAYOUT;
        case View.FOCUS_DOWN:
            return mOrientation == VERTICAL ? RenderState.LAYOUT_END
                    : RenderState.INVALID_LAYOUT;
        case View.FOCUS_LEFT:
            return mOrientation == HORIZONTAL ? RenderState.LAYOUT_START
                    : RenderState.INVALID_LAYOUT;
        case View.FOCUS_RIGHT:
            return mOrientation == HORIZONTAL ? RenderState.LAYOUT_END
                    : RenderState.INVALID_LAYOUT;
        default:
            if (DEBUG) {
                Log.d(TAG, "Unknown focus request:" + focusDirection);
            }
            return RenderState.INVALID_LAYOUT;
    }

}
 
Example 12
/**
 * Converts a focusDirection to orientation.
 *
 * @param focusDirection One of {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
 *                       {@link View#FOCUS_LEFT}, {@link View#FOCUS_RIGHT},
 *                       {@link View#FOCUS_BACKWARD}, {@link View#FOCUS_FORWARD}
 *                       or 0 for not applicable
 * @return {@link LayoutState#LAYOUT_START} or {@link LayoutState#LAYOUT_END} if focus direction
 * is applicable to current state, {@link LayoutState#INVALID_LAYOUT} otherwise.
 */
int convertFocusDirectionToLayoutDirection(int focusDirection) {
    switch (focusDirection) {
        case View.FOCUS_BACKWARD:
            if (mOrientation == VERTICAL) {
                return LayoutState.LAYOUT_START;
            } else if (isLayoutRTL()) {
                return LayoutState.LAYOUT_END;
            } else {
                return LayoutState.LAYOUT_START;
            }
        case View.FOCUS_FORWARD:
            if (mOrientation == VERTICAL) {
                return LayoutState.LAYOUT_END;
            } else if (isLayoutRTL()) {
                return LayoutState.LAYOUT_START;
            } else {
                return LayoutState.LAYOUT_END;
            }
        case View.FOCUS_UP:
            return mOrientation == VERTICAL ? LayoutState.LAYOUT_START
                    : LayoutState.INVALID_LAYOUT;
        case View.FOCUS_DOWN:
            return mOrientation == VERTICAL ? LayoutState.LAYOUT_END
                    : LayoutState.INVALID_LAYOUT;
        case View.FOCUS_LEFT:
            return mOrientation == HORIZONTAL ? LayoutState.LAYOUT_START
                    : LayoutState.INVALID_LAYOUT;
        case View.FOCUS_RIGHT:
            return mOrientation == HORIZONTAL ? LayoutState.LAYOUT_END
                    : LayoutState.INVALID_LAYOUT;
        default:
            if (DEBUG) {
                Log.d(TAG, "Unknown focus request:" + focusDirection);
            }
            return LayoutState.INVALID_LAYOUT;
    }

}
 
Example 13
/**
 * Converts a focusDirection to orientation.
 *
 * @param focusDirection One of {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
 *                       {@link View#FOCUS_LEFT}, {@link View#FOCUS_RIGHT},
 *                       {@link View#FOCUS_BACKWARD}, {@link View#FOCUS_FORWARD}
 *                       or 0 for not applicable
 * @return {@link LayoutState#LAYOUT_START} or {@link LayoutState#LAYOUT_END} if focus direction
 * is applicable to current state, {@link LayoutState#INVALID_LAYOUT} otherwise.
 */
private int convertFocusDirectionToLayoutDirection(int focusDirection) {
    switch (focusDirection) {
        case View.FOCUS_BACKWARD:
            if (mOrientation == VERTICAL) {
                return LayoutState.LAYOUT_START;
            } else if (isLayoutRTL()) {
                return LayoutState.LAYOUT_END;
            } else {
                return LayoutState.LAYOUT_START;
            }
        case View.FOCUS_FORWARD:
            if (mOrientation == VERTICAL) {
                return LayoutState.LAYOUT_END;
            } else if (isLayoutRTL()) {
                return LayoutState.LAYOUT_START;
            } else {
                return LayoutState.LAYOUT_END;
            }
        case View.FOCUS_UP:
            return mOrientation == VERTICAL ? LayoutState.LAYOUT_START
                    : LayoutState.INVALID_LAYOUT;
        case View.FOCUS_DOWN:
            return mOrientation == VERTICAL ? LayoutState.LAYOUT_END
                    : LayoutState.INVALID_LAYOUT;
        case View.FOCUS_LEFT:
            return mOrientation == HORIZONTAL ? LayoutState.LAYOUT_START
                    : LayoutState.INVALID_LAYOUT;
        case View.FOCUS_RIGHT:
            return mOrientation == HORIZONTAL ? LayoutState.LAYOUT_END
                    : LayoutState.INVALID_LAYOUT;
        default:
            if (DEBUG) {
                Log.d(TAG, "Unknown focus request:" + focusDirection);
            }
            return LayoutState.INVALID_LAYOUT;
    }

}
 
Example 14
/**
 * Converts a focusDirection to orientation.
 *
 * @param focusDirection One of {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
 *                       {@link View#FOCUS_LEFT}, {@link View#FOCUS_RIGHT},
 *                       {@link View#FOCUS_BACKWARD}, {@link View#FOCUS_FORWARD}
 *                       or 0 for not applicable
 * @return {@link LayoutState#LAYOUT_START} or {@link LayoutState#LAYOUT_END} if focus direction
 * is applicable to current state, {@link LayoutState#INVALID_LAYOUT} otherwise.
 */
int convertFocusDirectionToLayoutDirection(int focusDirection) {
    switch (focusDirection) {
        case View.FOCUS_BACKWARD:
            if (mOrientation == VERTICAL) {
                return LayoutState.LAYOUT_START;
            } else if (isLayoutRTL()) {
                return LayoutState.LAYOUT_END;
            } else {
                return LayoutState.LAYOUT_START;
            }
        case View.FOCUS_FORWARD:
            if (mOrientation == VERTICAL) {
                return LayoutState.LAYOUT_END;
            } else if (isLayoutRTL()) {
                return LayoutState.LAYOUT_START;
            } else {
                return LayoutState.LAYOUT_END;
            }
        case View.FOCUS_UP:
            return mOrientation == VERTICAL ? LayoutState.LAYOUT_START
                    : LayoutState.INVALID_LAYOUT;
        case View.FOCUS_DOWN:
            return mOrientation == VERTICAL ? LayoutState.LAYOUT_END
                    : LayoutState.INVALID_LAYOUT;
        case View.FOCUS_LEFT:
            return mOrientation == HORIZONTAL ? LayoutState.LAYOUT_START
                    : LayoutState.INVALID_LAYOUT;
        case View.FOCUS_RIGHT:
            return mOrientation == HORIZONTAL ? LayoutState.LAYOUT_END
                    : LayoutState.INVALID_LAYOUT;
        default:
            if (DEBUG) {
                Log.d(TAG, "Unknown focus request:" + focusDirection);
            }
            return LayoutState.INVALID_LAYOUT;
    }

}
 
Example 15
Source Project: android_9.0.0_r45   File: GridView.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Is childIndex a candidate for next focus given the direction the focus
 * change is coming from?
 * @param childIndex The index to check.
 * @param direction The direction, one of
 *        {FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD, FOCUS_BACKWARD}
 * @return Whether childIndex is a candidate.
 */
private boolean isCandidateSelection(int childIndex, int direction) {
    final int count = getChildCount();
    final int invertedIndex = count - 1 - childIndex;

    int rowStart;
    int rowEnd;

    if (!mStackFromBottom) {
        rowStart = childIndex - (childIndex % mNumColumns);
        rowEnd = Math.min(rowStart + mNumColumns - 1, count);
    } else {
        rowEnd = count - 1 - (invertedIndex - (invertedIndex % mNumColumns));
        rowStart = Math.max(0, rowEnd - mNumColumns + 1);
    }

    switch (direction) {
        case View.FOCUS_RIGHT:
            // coming from left, selection is only valid if it is on left
            // edge
            return childIndex == rowStart;
        case View.FOCUS_DOWN:
            // coming from top; only valid if in top row
            return rowStart == 0;
        case View.FOCUS_LEFT:
            // coming from right, must be on right edge
            return childIndex == rowEnd;
        case View.FOCUS_UP:
            // coming from bottom, need to be in last row
            return rowEnd == count - 1;
        case View.FOCUS_FORWARD:
            // coming from top-left, need to be first in top row
            return childIndex == rowStart && rowStart == 0;
        case View.FOCUS_BACKWARD:
            // coming from bottom-right, need to be last in bottom row
            return childIndex == rowEnd && rowEnd == count - 1;
        default:
            throw new IllegalArgumentException("direction must be one of "
                    + "{FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, "
                    + "FOCUS_FORWARD, FOCUS_BACKWARD}.");
    }
}
 
Example 16
public boolean arrowScroll(int direction) {
    View currentFocused = findFocus();
    if (currentFocused == this) {
        currentFocused = null;
    } else if (currentFocused != null) {
        boolean isChild = false;
        for (ViewParent parent = currentFocused.getParent(); parent instanceof ViewGroup;
             parent = parent.getParent()) {
            if (parent == this) {
                isChild = true;
                break;
            }
        }
        if (!isChild) {
            // This would cause the focus search down below to fail in fun ways.
            final StringBuilder sb = new StringBuilder();
            sb.append(currentFocused.getClass().getSimpleName());
            for (ViewParent parent = currentFocused.getParent(); parent instanceof ViewGroup;
                 parent = parent.getParent()) {
                sb.append(" => ").append(parent.getClass().getSimpleName());
            }
            Log.e(TAG, "arrowScroll tried to find focus based on non-child " +
                    "current focused view " + sb.toString());
            currentFocused = null;
        }
    }

    boolean handled = false;

    View nextFocused = FocusFinder.getInstance().findNextFocus(this, currentFocused,
            direction);
    if (nextFocused != null && nextFocused != currentFocused) {
        if (direction == View.FOCUS_UP) {
            // If there is nothing to the left, or this is causing us to
            // jump to the right, then what we really want to do is page left.
            final int nextTop = getChildRectInPagerCoordinates(mTempRect, nextFocused).top;
            final int currTop = getChildRectInPagerCoordinates(mTempRect, currentFocused).top;
            if (currentFocused != null && nextTop >= currTop) {
                handled = pageUp();
            } else {
                handled = nextFocused.requestFocus();
            }
        } else if (direction == View.FOCUS_DOWN) {
            // If there is nothing to the right, or this is causing us to
            // jump to the left, then what we really want to do is page right.
            final int nextDown = getChildRectInPagerCoordinates(mTempRect, nextFocused).bottom;
            final int currDown = getChildRectInPagerCoordinates(mTempRect, currentFocused).bottom;
            if (currentFocused != null && nextDown <= currDown) {
                handled = pageDown();
            } else {
                handled = nextFocused.requestFocus();
            }
        }
    } else if (direction == FOCUS_UP || direction == FOCUS_BACKWARD) {
        // Trying to move left and nothing there; try to page.
        handled = pageUp();
    } else if (direction == FOCUS_DOWN || direction == FOCUS_FORWARD) {
        // Trying to move right and nothing there; try to page.
        handled = pageDown();
    }
    if (handled) {
        playSoundEffect(SoundEffectConstants.getContantForFocusDirection(direction));
    }
    return handled;
}
 
Example 17
static int getDistance(Rect source, Rect dest, int direction) {
    int sX, sY;
    int dX, dY;
    switch (direction) {
    case View.FOCUS_RIGHT:
        sX = source.right;
        sY = source.top + source.height() / 2;
        dX = dest.left;
        dY = dest.top + dest.height() / 2;
        break;
    case View.FOCUS_DOWN:
        sX = source.left + source.width() / 2;
        sY = source.bottom;
        dX = dest.left + dest.width() / 2;
        dY = dest.top;
        break;
    case View.FOCUS_LEFT:
        sX = source.left;
        sY = source.top + source.height() / 2;
        dX = dest.right;
        dY = dest.top + dest.height() / 2;
        break;
    case View.FOCUS_UP:
        sX = source.left + source.width() / 2;
        sY = source.top;
        dX = dest.left + dest.width() / 2;
        dY = dest.bottom;
        break;
    case View.FOCUS_FORWARD:
    case View.FOCUS_BACKWARD:
        sX = source.right + source.width() / 2;
        sY = source.top + source.height() / 2;
        dX = dest.left + dest.width() / 2;
        dY = dest.top + dest.height() / 2;
        break;
    default:
        throw new IllegalArgumentException("direction must be one of "
                + "{FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, " +
                "FOCUS_FORWARD, FOCUS_BACKWARD}.");
    }
    int deltaX = dX - sX;
    int deltaY = dY - sY;
    return deltaY * deltaY + deltaX * deltaX;
}
 
Example 18
Source Project: SprintNBA   File: VerticalViewPager.java    License: Apache License 2.0 4 votes vote down vote up
public boolean arrowScroll(int direction) {
  View currentFocused = findFocus();
  if (currentFocused == this) {
    currentFocused = null;
  } else if (currentFocused != null) {
    boolean isChild = false;
    for (ViewParent parent = currentFocused.getParent(); parent instanceof ViewGroup;
        parent = parent.getParent()) {
      if (parent == this) {
        isChild = true;
        break;
      }
    }
    if (!isChild) {
      // This would cause the focus search down below to fail in fun ways.
      final StringBuilder sb = new StringBuilder();
      sb.append(currentFocused.getClass().getSimpleName());
      for (ViewParent parent = currentFocused.getParent(); parent instanceof ViewGroup;
          parent = parent.getParent()) {
        sb.append(" => ").append(parent.getClass().getSimpleName());
      }
      Log.e(TAG, "arrowScroll tried to find focus based on non-child " +
          "current focused view " + sb.toString());
      currentFocused = null;
    }
  }

  boolean handled = false;

  View nextFocused = FocusFinder.getInstance().findNextFocus(this, currentFocused, direction);
  if (nextFocused != null && nextFocused != currentFocused) {
    if (direction == View.FOCUS_UP) {
      // If there is nothing to the left, or this is causing us to
      // jump to the right, then what we really want to do is page left.
      final int nextTop = getChildRectInPagerCoordinates(mTempRect, nextFocused).top;
      final int currTop = getChildRectInPagerCoordinates(mTempRect, currentFocused).top;
      if (currentFocused != null && nextTop >= currTop) {
        handled = pageUp();
      } else {
        handled = nextFocused.requestFocus();
      }
    } else if (direction == View.FOCUS_DOWN) {
      // If there is nothing to the right, or this is causing us to
      // jump to the left, then what we really want to do is page right.
      final int nextDown = getChildRectInPagerCoordinates(mTempRect, nextFocused).bottom;
      final int currDown = getChildRectInPagerCoordinates(mTempRect, currentFocused).bottom;
      if (currentFocused != null && nextDown <= currDown) {
        handled = pageDown();
      } else {
        handled = nextFocused.requestFocus();
      }
    }
  } else if (direction == FOCUS_UP || direction == FOCUS_BACKWARD) {
    // Trying to move left and nothing there; try to page.
    handled = pageUp();
  } else if (direction == FOCUS_DOWN || direction == FOCUS_FORWARD) {
    // Trying to move right and nothing there; try to page.
    handled = pageDown();
  }
  if (handled) {
    playSoundEffect(SoundEffectConstants.getContantForFocusDirection(direction));
  }
  return handled;
}
 
Example 19
public boolean arrowScroll(int direction) {
    View currentFocused = findFocus();
    if (currentFocused == this) {
        currentFocused = null;
    } else if (currentFocused != null) {
        boolean isChild = false;
        for (ViewParent parent = currentFocused.getParent(); parent instanceof ViewGroup;
             parent = parent.getParent()) {
            if (parent == this) {
                isChild = true;
                break;
            }
        }
        if (!isChild) {
            // This would cause the focus search down below to fail in fun ways.
            final StringBuilder sb = new StringBuilder();
            sb.append(currentFocused.getClass().getSimpleName());
            for (ViewParent parent = currentFocused.getParent(); parent instanceof ViewGroup;
                 parent = parent.getParent()) {
                sb.append(" => ").append(parent.getClass().getSimpleName());
            }
            Log.e(TAG, "arrowScroll tried to find focus based on non-child " +
                    "current focused view " + sb.toString());
            currentFocused = null;
        }
    }

    boolean handled = false;

    View nextFocused = FocusFinder.getInstance().findNextFocus(this, currentFocused,
            direction);
    if (nextFocused != null && nextFocused != currentFocused) {
        if (direction == View.FOCUS_UP) {
            // If there is nothing to the left, or this is causing us to
            // jump to the right, then what we really want to do is page left.
            final int nextTop = getChildRectInPagerCoordinates(mTempRect, nextFocused).top;
            final int currTop = getChildRectInPagerCoordinates(mTempRect, currentFocused).top;
            if (currentFocused != null && nextTop >= currTop) {
                handled = pageUp();
            } else {
                handled = nextFocused.requestFocus();
            }
        } else if (direction == View.FOCUS_DOWN) {
            // If there is nothing to the right, or this is causing us to
            // jump to the left, then what we really want to do is page right.
            final int nextDown = getChildRectInPagerCoordinates(mTempRect, nextFocused).bottom;
            final int currDown = getChildRectInPagerCoordinates(mTempRect, currentFocused).bottom;
            if (currentFocused != null && nextDown <= currDown) {
                handled = pageDown();
            } else {
                handled = nextFocused.requestFocus();
            }
        }
    } else if (direction == FOCUS_UP || direction == FOCUS_BACKWARD) {
        // Trying to move left and nothing there; try to page.
        handled = pageUp();
    } else if (direction == FOCUS_DOWN || direction == FOCUS_FORWARD) {
        // Trying to move right and nothing there; try to page.
        handled = pageDown();
    }
    if (handled) {
        playSoundEffect(SoundEffectConstants.getContantForFocusDirection(direction));
    }
    return handled;
}
 
Example 20
public boolean arrowScroll(int direction) {
    View currentFocused = findFocus();
    if (currentFocused == this) {
        currentFocused = null;
    } else if (currentFocused != null) {
        boolean isChild = false;
        for (ViewParent parent = currentFocused.getParent(); parent instanceof ViewGroup;
                parent = parent.getParent()) {
            if (parent == this) {
                isChild = true;
                break;
            }
        }
        if (!isChild) {
            // This would cause the focus search down below to fail in fun ways.
            final StringBuilder sb = new StringBuilder();
            sb.append(currentFocused.getClass().getSimpleName());
            for (ViewParent parent = currentFocused.getParent(); parent instanceof ViewGroup;
                    parent = parent.getParent()) {
                sb.append(" => ").append(parent.getClass().getSimpleName());
            }
            Log.e(TAG, "arrowScroll tried to find focus based on non-child " +
                    "current focused view " + sb.toString());
            currentFocused = null;
        }
    }

    boolean handled = false;

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