Java Code Examples for android.view.View.FOCUS_UP

The following are Jave code examples for showing how to use FOCUS_UP of the android.view.View class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: exciting-app   File: HListView.java   View Source Code Vote up 7 votes
/**
 * Determine how much we need to scroll in order to get newFocus in view.
 * 
 * @param direction
 *            either {@link android.view.View#FOCUS_UP} or
 *            {@link android.view.View#FOCUS_DOWN}.
 * @param newFocus
 *            The view that would take focus.
 * @param positionOfNewFocus
 *            The position of the list item containing newFocus
 * @return The amount to scroll. Note: this is always positive! Direction
 *         needs to be taken into account when actually scrolling.
 */
private int amountToScrollToNewFocus(int direction, View newFocus, int positionOfNewFocus) {
	int amountToScroll = 0;
	newFocus.getDrawingRect(mTempRect);
	offsetDescendantRectToMyCoords(newFocus, mTempRect);
	if (direction == View.FOCUS_UP) {
		if (mTempRect.left < mListPadding.left) {
			amountToScroll = mListPadding.left - mTempRect.left;
			if (positionOfNewFocus > 0) {
				amountToScroll += getArrowScrollPreviewLength();
			}
		}
	} else {
		final int listRight = getWidth() - mListPadding.right;
		if (mTempRect.bottom > listRight) {
			amountToScroll = mTempRect.right - listRight;
			if (positionOfNewFocus < mItemCount - 1) {
				amountToScroll += getArrowScrollPreviewLength();
			}
		}
	}
	return amountToScroll;
}
 
Example 2
Project: javaide   File: TwoDScrollView.java   View Source Code Vote up 7 votes
/**
 * 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 3
Project: TvHelper   File: FocusBehaviourHandlerView.java   View Source Code Vote up 7 votes
/**
 * Logic taken from FocusSearch#isCandidate
 */
private boolean isPreferredNextFocusAbsolute(View focused, View next, int direction) {

    int[] location = getLocation(focused);
    int[] location2 = getLocation(next);
    mTempRect.set(location[0], location[1], location[0] + focused.getWidth(), location[1] + focused.getHeight());
    mTempRect2.set(location2[0], location2[1], location2[0] + next.getWidth(), location2[1] + next.getHeight());

    switch (direction) {
        case View.FOCUS_LEFT:
            return mTempRect.left >= mTempRect2.right;
        case View.FOCUS_RIGHT:
            return mTempRect.right <= mTempRect2.left;
        case View.FOCUS_UP:
            return mTempRect.top >= mTempRect2.bottom;
        case View.FOCUS_DOWN:
            return mTempRect.bottom <= mTempRect2.top;
    }
    throw new IllegalArgumentException("direction must be absolute. received:" + direction);
}
 
Example 4
Project: GitHub   File: ExposeLinearLayoutManagerEx.java   View Source Code Vote up 6 votes
/**
 * 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 convertFocusDirectionToLayoutDirectionExpose(int focusDirection) {
    int orientation = getOrientation();
    switch (focusDirection) {
        case View.FOCUS_BACKWARD:
            return LayoutState.LAYOUT_START;
        case View.FOCUS_FORWARD:
            return LayoutState.LAYOUT_END;
        case View.FOCUS_UP:
            return orientation == VERTICAL ? LayoutState.LAYOUT_START
                    : LayoutState.INVALID_LAYOUT;
        case View.FOCUS_DOWN:
            return orientation == VERTICAL ? LayoutState.LAYOUT_END
                    : LayoutState.INVALID_LAYOUT;
        case View.FOCUS_LEFT:
            return orientation == HORIZONTAL ? LayoutState.LAYOUT_START
                    : LayoutState.INVALID_LAYOUT;
        case View.FOCUS_RIGHT:
            return orientation == HORIZONTAL ? LayoutState.LAYOUT_END
                    : LayoutState.INVALID_LAYOUT;
        default:
            if (DEBUG) {
                Log.d(TAG, "Unknown focus request:" + focusDirection);
            }
            return LayoutState.INVALID_LAYOUT;
    }

}
 
Example 5
Project: GitHub   File: ExposeLinearLayoutManagerEx.java   View Source Code Vote up 6 votes
/**
 * 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 convertFocusDirectionToLayoutDirectionExpose(int focusDirection) {
    int orientation = getOrientation();
    switch (focusDirection) {
        case View.FOCUS_BACKWARD:
            return LayoutState.LAYOUT_START;
        case View.FOCUS_FORWARD:
            return LayoutState.LAYOUT_END;
        case View.FOCUS_UP:
            return orientation == VERTICAL ? LayoutState.LAYOUT_START
                    : LayoutState.INVALID_LAYOUT;
        case View.FOCUS_DOWN:
            return orientation == VERTICAL ? LayoutState.LAYOUT_END
                    : LayoutState.INVALID_LAYOUT;
        case View.FOCUS_LEFT:
            return orientation == HORIZONTAL ? LayoutState.LAYOUT_START
                    : LayoutState.INVALID_LAYOUT;
        case View.FOCUS_RIGHT:
            return orientation == HORIZONTAL ? LayoutState.LAYOUT_END
                    : LayoutState.INVALID_LAYOUT;
        default:
            if (DEBUG) {
                Log.d(TAG, "Unknown focus request:" + focusDirection);
            }
            return LayoutState.INVALID_LAYOUT;
    }

}
 
Example 6
Project: airgram   File: StaggeredGridLayoutManager.java   View Source Code Vote up 6 votes
/**
 * 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 7
Project: airgram   File: LinearLayoutManager.java   View Source Code Vote up 6 votes
/**
 * 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:
            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 8
Project: vlayout   File: ExposeLinearLayoutManagerEx.java   View Source Code Vote up 6 votes
/**
 * 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 convertFocusDirectionToLayoutDirectionExpose(int focusDirection) {
    int orientation = getOrientation();
    switch (focusDirection) {
        case View.FOCUS_BACKWARD:
            return LayoutState.LAYOUT_START;
        case View.FOCUS_FORWARD:
            return LayoutState.LAYOUT_END;
        case View.FOCUS_UP:
            return orientation == VERTICAL ? LayoutState.LAYOUT_START
                    : LayoutState.INVALID_LAYOUT;
        case View.FOCUS_DOWN:
            return orientation == VERTICAL ? LayoutState.LAYOUT_END
                    : LayoutState.INVALID_LAYOUT;
        case View.FOCUS_LEFT:
            return orientation == HORIZONTAL ? LayoutState.LAYOUT_START
                    : LayoutState.INVALID_LAYOUT;
        case View.FOCUS_RIGHT:
            return orientation == HORIZONTAL ? LayoutState.LAYOUT_END
                    : LayoutState.INVALID_LAYOUT;
        default:
            if (DEBUG) {
                Log.d(TAG, "Unknown focus request:" + focusDirection);
            }
            return LayoutState.INVALID_LAYOUT;
    }

}
 
Example 9
Project: exciting-app   File: HListView.java   View Source Code Vote up 6 votes
/**
 * To avoid horizontal focus searches changing the selected item, we
 * manually focus search within the selected item (as applicable), and
 * prevent focus from jumping to something within another item.
 * 
 * @param direction
 *            one of {View.FOCUS_LEFT, View.FOCUS_RIGHT}
 * @return Whether this consumes the key event.
 */
private boolean handleHorizontalFocusWithinListItem(int direction) {
	// TODO: implement this
	if (direction != View.FOCUS_UP && direction != View.FOCUS_DOWN) {
		throw new IllegalArgumentException("direction must be one of" + " {View.FOCUS_UP, View.FOCUS_DOWN}");
	}

	final int numChildren = getChildCount();
	if (mItemsCanFocus && numChildren > 0 && mSelectedPosition != INVALID_POSITION) {
		final View selectedView = getSelectedView();
		if (selectedView != null && selectedView.hasFocus() && selectedView instanceof ViewGroup) {

			final View currentFocus = selectedView.findFocus();
			final View nextFocus = FocusFinder.getInstance().findNextFocus((ViewGroup) selectedView, currentFocus,
					direction);
			if (nextFocus != null) {
				// do the math to get interesting rect in next focus'
				// coordinates
				currentFocus.getFocusedRect(mTempRect);
				offsetDescendantRectToMyCoords(currentFocus, mTempRect);
				offsetRectIntoDescendantCoords(nextFocus, mTempRect);
				if (nextFocus.requestFocus(direction, mTempRect)) {
					return true;
				}
			}
			// we are blocking the key from being handled (by returning
			// true)
			// if the global result is going to be some other view within
			// this
			// list. this is to acheive the overall goal of having
			// horizontal d-pad navigation remain in the current item.
			final View globalNextFocus = FocusFinder.getInstance().findNextFocus((ViewGroup) getRootView(),
					currentFocus, direction);
			if (globalNextFocus != null) {
				return isViewAncestorOf(globalNextFocus, this);
			}
		}
	}
	return false;
}
 
Example 10
Project: javaide   File: TwoDScrollView.java   View Source Code Vote up 6 votes
/**
 * <p>Scrolls the view to make the area defined by <code>top</code> and
 * <code>bottom</code> visible. This method attempts to give the focus
 * to a component visible in this area. If no component can be focused in
 * the new visible area, the focus is reclaimed by this scrollview.</p>
 *
 * @param direction the scroll direction: {@link android.view.View#FOCUS_UP}
 *                  to go upward
 *                  {@link android.view.View#FOCUS_DOWN} to downward
 * @param top       the top offset of the new area to be made visible
 * @param bottom    the bottom offset of the new area to be made visible
 * @return true if the key event is consumed by this method, false otherwise
 */
private boolean scrollAndFocus(int directionY, int top, int bottom, int directionX, int left, int right) {
    boolean handled = true;
    int height = getHeight();
    int containerTop = getScrollY();
    int containerBottom = containerTop + height;
    boolean up = directionY == View.FOCUS_UP;
    int width = getWidth();
    int containerLeft = getScrollX();
    int containerRight = containerLeft + width;
    boolean leftwards = directionX == View.FOCUS_UP;
    View newFocused = findFocusableViewInBounds(up, top, bottom, leftwards, left, right);
    if (newFocused == null) {
        newFocused = this;
    }
    if ((top >= containerTop && bottom <= containerBottom) || (left >= containerLeft && right <= containerRight)) {
        handled = false;
    } else {
        int deltaY = up ? (top - containerTop) : (bottom - containerBottom);
        int deltaX = leftwards ? (left - containerLeft) : (right - containerRight);
        doScroll(deltaX, deltaY);
    }
    if (newFocused != findFocus() && newFocused.requestFocus(directionY)) {
        mTwoDScrollViewMovedFocus = true;
        mTwoDScrollViewMovedFocus = false;
    }
    return handled;
}
 
Example 11
Project: aos-Video   File: TitleHelper.java   View Source Code Vote up 6 votes
@Override
public View onFocusSearch(View focused, int direction) {
    if (focused != mTitleView && direction == View.FOCUS_UP) {
        return mTitleView;
    }
    final boolean isRtl = ViewCompat.getLayoutDirection(focused) ==
            View.LAYOUT_DIRECTION_RTL;
    //final int forward = isRtl ? View.FOCUS_LEFT : View.FOCUS_RIGHT;
    if (mTitleView.hasFocus() && direction == View.FOCUS_DOWN /*|| direction == forward*/) {
        return mSceneRoot;
    }
    return null;
}
 
Example 12
Project: AndroidTvDemo   File: FocusGridLayoutManager.java   View Source Code Vote up 6 votes
/**
 * Calculates position offset.
 *
 * @param direction regular {@code View.FOCUS_*}.
 * @return position offset according to {@code direction}.
 */
protected int calcOffsetToNextView(int direction)
{
    int spanCount = getSpanCount();
    int orientation = getOrientation();
    
    if (orientation == VERTICAL)
    {
        switch (direction)
        {
            case View.FOCUS_DOWN:
                return spanCount;
            case View.FOCUS_UP:
                return -spanCount;
            case View.FOCUS_RIGHT:
                return 1;
            case View.FOCUS_LEFT:
                return -1;
        }
    }
    else if (orientation == HORIZONTAL)
    {
        switch (direction)
        {
            case View.FOCUS_DOWN:
                return 1;
            case View.FOCUS_UP:
                return -1;
            case View.FOCUS_RIGHT:
                return spanCount;
            case View.FOCUS_LEFT:
                return -spanCount;
        }
    }
    
    return 0;
}
 
Example 13
Project: PlusGram   File: StaggeredGridLayoutManager.java   View Source Code Vote up 6 votes
/**
 * 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 14
Project: PlusGram   File: LinearLayoutManager.java   View Source Code Vote up 6 votes
/**
 * 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:
            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 15
Project: microMathematics   File: TwoDScrollView.java   View Source Code Vote up 6 votes
/**
 * Scrolls the view to make the area defined by <code>top</code> and <code>bottom</code> visible. This method
 * attempts to give the focus to a component visible in this area. If no component can be focused in the new visible
 * area, the focus is reclaimed by this scrollview.
 *
 * @param directionY the scroll direction: {@link android.view.View#FOCUS_UP} to go upward
 *                  {@link android.view.View#FOCUS_DOWN} to downward
 * @param top       the top offset of the new area to be made visible
 * @param bottom    the bottom offset of the new area to be made visible
 * @return true if the key event is consumed by this method, false otherwise
 */
private boolean scrollAndFocus(int directionY, int top, int bottom, int directionX, int left, int right)
{
    boolean handled = true;
    int height = getHeight();
    int containerTop = getScrollY();
    int containerBottom = containerTop + height;
    boolean up = directionY == View.FOCUS_UP;
    int width = getWidth();
    int containerLeft = getScrollX();
    int containerRight = containerLeft + width;
    boolean leftwards = directionX == View.FOCUS_UP;
    View newFocused = findFocusableViewInBounds(up, top, bottom, leftwards, left, right);
    if (newFocused == null)
    {
        newFocused = this;
    }
    if ((top >= containerTop && bottom <= containerBottom) || (left >= containerLeft && right <= containerRight))
    {
        handled = false;
    }
    else
    {
        int deltaY = up ? (top - containerTop) : (bottom - containerBottom);
        int deltaX = leftwards ? (left - containerLeft) : (right - containerRight);
        doScroll(deltaX, deltaY);
    }
    return handled;
}
 
Example 16
Project: Swap   File: PLA_AbsListView.java   View Source Code Vote up 6 votes
/**
 * What is the distance between the source and destination rectangles given the direction of
 * focus navigation between them? The direction basically helps figure out more quickly what is
 * self evident by the relationship between the rects...
 *
 * @param source the source rectangle
 * @param dest the destination rectangle
 * @param direction the direction
 * @return the distance between the rectangles
 */
static int getDistance(Rect source, Rect dest, int direction) {
    int sX, sY; // source x, y
    int dX, dY; // dest x, y
    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;
        default:
            throw new IllegalArgumentException("direction must be one of "
                    + "{FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT}.");
    }
    int deltaX = dX - sX;
    int deltaY = dY - sY;
    return deltaY * deltaY + deltaX * deltaX;
}
 
Example 17
Project: GitHub   File: VerticalViewPager.java   View Source Code Vote up 5 votes
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 18
Project: ChangeTabLayout   File: VerticalViewPager.java   View Source Code Vote up 5 votes
/**
 * Handle scrolling in response to a left or right arrow click.
 *
 * @param direction The direction corresponding to the arrow key that was pressed. It should be
 *                  either {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}.
 * @return Whether the scrolling was handled successfully.
 */
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
Project: exciting-app   File: HListView.java   View Source Code Vote up 5 votes
/**
 * When selection changes, it is possible that the previously selected or
 * the next selected item will change its size. If so, we need to offset
 * some folks, and re-layout the items as appropriate.
 * 
 * @param selectedView
 *            The currently selected view (before changing selection).
 *            should be <code>null</code> if there was no previous
 *            selection.
 * @param direction
 *            Either {@link android.view.View#FOCUS_UP} or
 *            {@link android.view.View#FOCUS_DOWN}.
 * @param newSelectedPosition
 *            The position of the next selection.
 * @param newFocusAssigned
 *            whether new focus was assigned. This matters because when
 *            something has focus, we don't want to show selection (ugh).
 */
private void handleNewSelectionChange(View selectedView, int direction, int newSelectedPosition,
		boolean newFocusAssigned) {
	if (newSelectedPosition == INVALID_POSITION) {
		throw new IllegalArgumentException("newSelectedPosition needs to be valid");
	}

	// whether or not we are moving down or up, we want to preserve the
	// top of whatever view is on top:
	// - moving down: the view that had selection
	// - moving up: the view that is getting selection
	View leftView;
	View rightView;
	int leftViewIndex, rightViewIndex;
	boolean leftSelected = false;
	final int selectedIndex = mSelectedPosition - mFirstPosition;
	final int nextSelectedIndex = newSelectedPosition - mFirstPosition;

	if (direction == View.FOCUS_UP) {
		leftViewIndex = nextSelectedIndex;
		rightViewIndex = selectedIndex;
		leftView = getChildAt(leftViewIndex);
		rightView = selectedView;
		leftSelected = true;
	} else {
		leftViewIndex = selectedIndex;
		rightViewIndex = nextSelectedIndex;
		leftView = selectedView;
		rightView = getChildAt(rightViewIndex);
	}

	final int numChildren = getChildCount();

	// start with top view: is it changing size?
	if (leftView != null) {
		leftView.setSelected(!newFocusAssigned && leftSelected);
		measureAndAdjustRight(leftView, leftViewIndex, numChildren);
	}

	// is the bottom view changing size?
	if (rightView != null) {
		rightView.setSelected(!newFocusAssigned && !leftSelected);
		measureAndAdjustRight(rightView, rightViewIndex, numChildren);
	}
}
 
Example 20
Project: exciting-app   File: AbsHListView.java   View Source Code Vote up 5 votes
/**
 * What is the distance between the source and destination rectangles given
 * the direction of focus navigation between them? The direction basically
 * helps figure out more quickly what is self evident by the relationship
 * between the rects...
 * 
 * @param source
 *            the source rectangle
 * @param dest
 *            the destination rectangle
 * @param direction
 *            the direction
 * @return the distance between the rectangles
 */
public static int getDistance(Rect source, Rect dest, int direction) {

	// TODO: implement this

	int sX, sY; // source x, y
	int dX, dY; // dest x, y
	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;
}