android.view.FocusFinder Java Examples

The following examples show how to use android.view.FocusFinder. 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 File: TwoDScrollView.java    From ssj with GNU General Public License v3.0 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);

    return nextFocus != null && nextFocus.requestFocus(direction, previouslyFocusedRect);
}
 
Example #2
Source File: ScrollViewer.java    From AirFree-Client with GNU General Public License v3.0 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.
 *
 * This is more expensive than the default {@link 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
Source File: Focus2AndroidTest.java    From codeexamples-android with Eclipse Public License 1.0 6 votes vote down vote up
@Override
protected void setUp() throws Exception {
    super.setUp();

    mFocusFinder = FocusFinder.getInstance();

    // inflate the layout
    final Context context = getContext();
    final LayoutInflater inflater = LayoutInflater.from(context);
    mRoot = (ViewGroup) inflater.inflate(R.layout.focus_2, null);

    // manually measure it, and lay it out
    mRoot.measure(500, 500);
    mRoot.layout(0, 0, 500, 500);

    mLeftButton = (Button) mRoot.findViewById(R.id.leftButton);
    mCenterButton = (Button) mRoot.findViewById(R.id.centerButton);
    mRightButton = (Button) mRoot.findViewById(R.id.rightButton);
}
 
Example #4
Source File: HListView.java    From letv with Apache License 2.0 6 votes vote down vote up
private boolean handleHorizontalFocusWithinListItem(int direction) {
    if (direction == 33 || direction == 130) {
        int numChildren = getChildCount();
        if (this.mItemsCanFocus && numChildren > 0 && this.mSelectedPosition != -1) {
            View selectedView = getSelectedView();
            if (selectedView != null && selectedView.hasFocus() && (selectedView instanceof ViewGroup)) {
                View currentFocus = selectedView.findFocus();
                View nextFocus = FocusFinder.getInstance().findNextFocus((ViewGroup) selectedView, currentFocus, direction);
                if (nextFocus != null) {
                    currentFocus.getFocusedRect(this.mTempRect);
                    offsetDescendantRectToMyCoords(currentFocus, this.mTempRect);
                    offsetRectIntoDescendantCoords(nextFocus, this.mTempRect);
                    if (nextFocus.requestFocus(direction, this.mTempRect)) {
                        return true;
                    }
                }
                View globalNextFocus = FocusFinder.getInstance().findNextFocus((ViewGroup) getRootView(), currentFocus, direction);
                if (globalNextFocus != null) {
                    return isViewAncestorOf(globalNextFocus, this);
                }
            }
        }
        return false;
    }
    throw new IllegalArgumentException("direction must be one of {View.FOCUS_UP, View.FOCUS_DOWN}");
}
 
Example #5
Source File: TabHorizontalGridView.java    From LeanbackTvSample with MIT License 6 votes vote down vote up
@Override
public View focusSearch(View focused, int direction) {
    if (focused != null) {
        final FocusFinder ff = FocusFinder.getInstance();
        final View found = ff.findNextFocus(this, focused, direction);
        if (direction == View.FOCUS_LEFT || direction == View.FOCUS_RIGHT) {
            if ((found == null || found.getId() != R.id.tv_main_title)
                    && getScrollState() == SCROLL_STATE_IDLE) {
                if (shakeX == null) {
                    shakeX = AnimationUtils.loadAnimation(getContext(), R.anim.host_shake);
                }
                focused.clearAnimation();
                focused.startAnimation(shakeX);
                return null;
            }
        }
    }
    return super.focusSearch(focused, direction);
}
 
Example #6
Source File: TwoDScrollView.java    From AndroidQuick with 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 #7
Source File: MyScrollView.java    From prayer-times-android with Apache License 2.0 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 ViewGroup}
 * implementation, otherwise this behavior might have been made the default.
 */
@Override
protected boolean onRequestFocusInDescendants(int direction, @Nullable 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;
    }

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

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

    return !isOffScreen(nextFocus) && nextFocus.requestFocus(direction, previouslyFocusedRect);

}
 
Example #8
Source File: TwoDScrollView.java    From javaide with GNU General Public License v3.0 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 #9
Source File: TwoDScrollView.java    From AndroidTreeView with Apache License 2.0 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 #10
Source File: HVScrollView.java    From JsDroidEditor with Mozilla Public License 2.0 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_RIGHT;
    // } else if (direction == View.FOCUS_BACKWARD) {
    // direction = View.FOCUS_LEFT;
    // }

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

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

    // if (isOffScreenH(nextFocus)) {
    // return false;
    // }

    return nextFocus.requestFocus(direction, previouslyFocusedRect);
}
 
Example #11
Source File: MyScrollView.java    From prayer-times-android with Apache License 2.0 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 ViewGroup}
 * implementation, otherwise this behavior might have been made the default.
 */
@Override
protected boolean onRequestFocusInDescendants(int direction, @Nullable 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;
    }

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

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

    return !isOffScreen(nextFocus) && nextFocus.requestFocus(direction, previouslyFocusedRect);

}
 
Example #12
Source File: TwoDScrollView.java    From imsdk-android with 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 #13
Source File: TwoDScrollView.java    From java-n-IDE-for-Android with Apache License 2.0 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 #14
Source File: CustomViewAbove.java    From WeatherStream with Apache License 2.0 5 votes vote down vote up
public boolean arrowScroll(int direction) {
	View currentFocused = findFocus();
	if (currentFocused == this) currentFocused = null;

	boolean handled = false;

	View nextFocused = FocusFinder.getInstance().findNextFocus(this, currentFocused,
			direction);
	if (nextFocused != null && nextFocused != currentFocused) {
		if (direction == View.FOCUS_LEFT) {
			handled = nextFocused.requestFocus();
		} else if (direction == View.FOCUS_RIGHT) {
			// 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.
			if (currentFocused != null && nextFocused.getLeft() <= currentFocused.getLeft()) {
				handled = pageRight();
			} else {
				handled = nextFocused.requestFocus();
			}
		}
	} else if (direction == FOCUS_LEFT || direction == FOCUS_BACKWARD) {
		// Trying to move left and nothing there; try to page.
		handled = pageLeft();
	} else if (direction == FOCUS_RIGHT || direction == FOCUS_FORWARD) {
		// Trying to move right and nothing there; try to page.
		handled = pageRight();
	}
	if (handled) {
		playSoundEffect(SoundEffectConstants.getContantForFocusDirection(direction));
	}
	return handled;
}
 
Example #15
Source File: DpadAwareRecyclerView.java    From dpad-aware-recycler-view with Apache License 2.0 5 votes vote down vote up
/**
 * Request natural focus.
 *
 * @param direction             direction in which focus is changing
 * @param previouslyFocusedRect previously focus rectangle
 */
private void requestNaturalFocus(int direction, Rect previouslyFocusedRect) {
    FocusFinder ff = FocusFinder.getInstance();
    previouslyFocusedRect = previouslyFocusedRect == null
            ? new Rect(0, 0, 0, 0) : previouslyFocusedRect;
    View toFocus = ff.findNextFocusFromRect(this, previouslyFocusedRect, direction);
    toFocus = toFocus == null ? getChildAt(0) : toFocus;
    if (toFocus != null) {
        toFocus.requestFocus();
    }
}
 
Example #16
Source File: AnScrollView.java    From Animer with Apache License 2.0 5 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.
 *
 * 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;
    }

    if (isOffScreen(nextFocus)) {
        return false;
    }

    return nextFocus.requestFocus(direction, previouslyFocusedRect);
}
 
Example #17
Source File: CustomViewAbove.java    From LiuAGeAndroid with MIT License 5 votes vote down vote up
public boolean arrowScroll(int direction) {
	View currentFocused = findFocus();
	if (currentFocused == this) currentFocused = null;

	boolean handled = false;

	View nextFocused = FocusFinder.getInstance().findNextFocus(this, currentFocused,
			direction);
	if (nextFocused != null && nextFocused != currentFocused) {
		if (direction == View.FOCUS_LEFT) {
			handled = nextFocused.requestFocus();
		} else if (direction == View.FOCUS_RIGHT) {
			// 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.
			if (currentFocused != null && nextFocused.getLeft() <= currentFocused.getLeft()) {
				handled = pageRight();
			} else {
				handled = nextFocused.requestFocus();
			}
		}
	} else if (direction == FOCUS_LEFT || direction == FOCUS_BACKWARD) {
		// Trying to move left and nothing there; try to page.
		handled = pageLeft();
	} else if (direction == FOCUS_RIGHT || direction == FOCUS_FORWARD) {
		// Trying to move right and nothing there; try to page.
		handled = pageRight();
	}
	if (handled) {
		playSoundEffect(SoundEffectConstants.getContantForFocusDirection(direction));
	}
	return handled;
}
 
Example #18
Source File: HListView.java    From Klyph with MIT License 5 votes vote down vote up
/**
 * 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_LEFT && direction != View.FOCUS_RIGHT ) {
		throw new IllegalArgumentException( "direction must be one of"
				+ " {View.FOCUS_LEFT, View.FOCUS_RIGHT}" );
	}

	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 #19
Source File: CustomViewAbove.java    From wakao-app with MIT License 5 votes vote down vote up
public boolean arrowScroll(int direction) {
	View currentFocused = findFocus();
	if (currentFocused == this) currentFocused = null;

	boolean handled = false;

	View nextFocused = FocusFinder.getInstance().findNextFocus(this, currentFocused,
			direction);
	if (nextFocused != null && nextFocused != currentFocused) {
		if (direction == View.FOCUS_LEFT) {
			handled = nextFocused.requestFocus();
		} else if (direction == View.FOCUS_RIGHT) {
			// 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.
			if (currentFocused != null && nextFocused.getLeft() <= currentFocused.getLeft()) {
				handled = pageRight();
			} else {
				handled = nextFocused.requestFocus();
			}
		}
	} else if (direction == FOCUS_LEFT || direction == FOCUS_BACKWARD) {
		// Trying to move left and nothing there; try to page.
		handled = pageLeft();
	} else if (direction == FOCUS_RIGHT || direction == FOCUS_FORWARD) {
		// Trying to move right and nothing there; try to page.
		handled = pageRight();
	}
	if (handled) {
		playSoundEffect(SoundEffectConstants.getContantForFocusDirection(direction));
	}
	return handled;
}
 
Example #20
Source File: NestedScrollView.java    From letv with Apache License 2.0 5 votes vote down vote up
public boolean executeKeyEvent(KeyEvent event) {
    this.mTempRect.setEmpty();
    if (canScroll()) {
        boolean handled = false;
        if (event.getAction() == 0) {
            switch (event.getKeyCode()) {
                case 19:
                    if (!event.isAltPressed()) {
                        handled = arrowScroll(33);
                        break;
                    }
                    handled = fullScroll(33);
                    break;
                case 20:
                    if (!event.isAltPressed()) {
                        handled = arrowScroll(130);
                        break;
                    }
                    handled = fullScroll(130);
                    break;
                case 62:
                    pageScroll(event.isShiftPressed() ? 33 : 130);
                    break;
            }
        }
        return handled;
    } else if (!isFocused() || event.getKeyCode() == 4) {
        return false;
    } else {
        View currentFocused = findFocus();
        if (currentFocused == this) {
            currentFocused = null;
        }
        View nextFocused = FocusFinder.getInstance().findNextFocus(this, currentFocused, 130);
        if (nextFocused == null || nextFocused == this || !nextFocused.requestFocus(130)) {
            return false;
        }
        return true;
    }
}
 
Example #21
Source File: CustomViewAbove.java    From KickAssSlidingMenu with Apache License 2.0 5 votes vote down vote up
public boolean arrowScroll(int direction) {
    View currentFocused = findFocus();
    if (currentFocused == this) currentFocused = null;

    boolean handled = false;

    View nextFocused = FocusFinder.getInstance().findNextFocus(this, currentFocused,
            direction);
    if (nextFocused != null && nextFocused != currentFocused) {
        if (direction == View.FOCUS_LEFT) {
            handled = nextFocused.requestFocus();
        } else if (direction == View.FOCUS_RIGHT) {
            // 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.
            if (currentFocused != null && nextFocused.getLeft() <= currentFocused.getLeft()) {
                handled = pageRight();
            } else {
                handled = nextFocused.requestFocus();
            }
        }
    } else if (direction == FOCUS_LEFT || direction == FOCUS_BACKWARD) {
        // Trying to move left and nothing there; try to page.
        handled = pageLeft();
    } else if (direction == FOCUS_RIGHT || direction == FOCUS_FORWARD) {
        // Trying to move right and nothing there; try to page.
        handled = pageRight();
    }
    if (handled) {
        playSoundEffect(SoundEffectConstants.getContantForFocusDirection(direction));
    }
    return handled;
}
 
Example #22
Source File: TwoDScrollView.java    From intra42 with Apache License 2.0 5 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 ViewGroup} implementation,
 * otherwise this behavior might have been made the default.</p>
 */
@Override
protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
    // convert from forward / backward notation to up / down / left / right
    if (direction == View.FOCUS_FORWARD) {
        direction = View.FOCUS_DOWN;
    } else if (direction == View.FOCUS_BACKWARD) {
        direction = View.FOCUS_UP;
    }

    View nextFocus = previouslyFocusedRect == null ? FocusFinder.getInstance().findNextFocus(this, null, direction) :
            FocusFinder.getInstance().findNextFocusFromRect(this, previouslyFocusedRect, direction);
    return nextFocus != null && nextFocus.requestFocus(direction, previouslyFocusedRect);
}
 
Example #23
Source File: NestedScrollView.java    From AndroidAnimationExercise with Apache License 2.0 5 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.
 *
 * This is more expensive than the default {@link 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;
    }

    if (isOffScreen(nextFocus)) {
        return false;
    }

    return nextFocus.requestFocus(direction, previouslyFocusedRect);
}
 
Example #24
Source File: BothScrollView.java    From Nimingban with Apache License 2.0 5 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.
 *
 * 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_RIGHT;
    } else if (direction == View.FOCUS_BACKWARD) {
        direction = View.FOCUS_LEFT;
    }

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

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

    if (isOffScreen(nextFocus)) {
        return false;
    }

    return nextFocus.requestFocus(direction, previouslyFocusedRect);
}
 
Example #25
Source File: CustomViewAbove.java    From BigApp_Discuz_Android with Apache License 2.0 5 votes vote down vote up
public boolean arrowScroll(int direction) {
	View currentFocused = findFocus();
	if (currentFocused == this) currentFocused = null;

	boolean handled = false;

	View nextFocused = FocusFinder.getInstance().findNextFocus(this, currentFocused,
			direction);
	if (nextFocused != null && nextFocused != currentFocused) {
		if (direction == View.FOCUS_LEFT) {
			handled = nextFocused.requestFocus();
		} else if (direction == View.FOCUS_RIGHT) {
			// 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.
			if (currentFocused != null && nextFocused.getLeft() <= currentFocused.getLeft()) {
				handled = pageRight();
			} else {
				handled = nextFocused.requestFocus();
			}
		}
	} else if (direction == FOCUS_LEFT || direction == FOCUS_BACKWARD) {
		// Trying to move left and nothing there; try to page.
		handled = pageLeft();
	} else if (direction == FOCUS_RIGHT || direction == FOCUS_FORWARD) {
		// Trying to move right and nothing there; try to page.
		handled = pageRight();
	}
	if (handled) {
		playSoundEffect(SoundEffectConstants.getContantForFocusDirection(direction));
	}
	return handled;
}
 
Example #26
Source File: CustomViewAbove.java    From Moring-Alarm with Apache License 2.0 5 votes vote down vote up
public boolean arrowScroll(int direction) {
	View currentFocused = findFocus();
	if (currentFocused == this) currentFocused = null;

	boolean handled = false;

	View nextFocused = FocusFinder.getInstance().findNextFocus(this, currentFocused,
			direction);
	if (nextFocused != null && nextFocused != currentFocused) {
		if (direction == View.FOCUS_LEFT) {
			handled = nextFocused.requestFocus();
		} else if (direction == View.FOCUS_RIGHT) {
			// 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.
			if (currentFocused != null && nextFocused.getLeft() <= currentFocused.getLeft()) {
				handled = pageRight();
			} else {
				handled = nextFocused.requestFocus();
			}
		}
	} else if (direction == FOCUS_LEFT || direction == FOCUS_BACKWARD) {
		// Trying to move left and nothing there; try to page.
		handled = pageLeft();
	} else if (direction == FOCUS_RIGHT || direction == FOCUS_FORWARD) {
		// Trying to move right and nothing there; try to page.
		handled = pageRight();
	}
	if (handled) {
		playSoundEffect(SoundEffectConstants.getContantForFocusDirection(direction));
	}
	return handled;
}
 
Example #27
Source File: CustomViewAbove.java    From BigApp_WordPress_Android with Apache License 2.0 5 votes vote down vote up
public boolean arrowScroll(int direction) {
	View currentFocused = findFocus();
	if (currentFocused == this) currentFocused = null;

	boolean handled = false;

	View nextFocused = FocusFinder.getInstance().findNextFocus(this, currentFocused,
			direction);
	if (nextFocused != null && nextFocused != currentFocused) {
		if (direction == View.FOCUS_LEFT) {
			handled = nextFocused.requestFocus();
		} else if (direction == View.FOCUS_RIGHT) {
			// 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.
			if (currentFocused != null && nextFocused.getLeft() <= currentFocused.getLeft()) {
				handled = pageRight();
			} else {
				handled = nextFocused.requestFocus();
			}
		}
	} else if (direction == FOCUS_LEFT || direction == FOCUS_BACKWARD) {
		// Trying to move left and nothing there; try to page.
		handled = pageLeft();
	} else if (direction == FOCUS_RIGHT || direction == FOCUS_FORWARD) {
		// Trying to move right and nothing there; try to page.
		handled = pageRight();
	}
	if (handled) {
		playSoundEffect(SoundEffectConstants.getContantForFocusDirection(direction));
	}
	return handled;
}
 
Example #28
Source File: CustomViewAbove.java    From Study_Android_Demo with Apache License 2.0 5 votes vote down vote up
public boolean arrowScroll(int direction) {
	View currentFocused = findFocus();
	if (currentFocused == this) currentFocused = null;

	boolean handled = false;

	View nextFocused = FocusFinder.getInstance().findNextFocus(this, currentFocused,
			direction);
	if (nextFocused != null && nextFocused != currentFocused) {
		if (direction == View.FOCUS_LEFT) {
			handled = nextFocused.requestFocus();
		} else if (direction == View.FOCUS_RIGHT) {
			// 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.
			if (currentFocused != null && nextFocused.getLeft() <= currentFocused.getLeft()) {
				handled = pageRight();
			} else {
				handled = nextFocused.requestFocus();
			}
		}
	} else if (direction == FOCUS_LEFT || direction == FOCUS_BACKWARD) {
		// Trying to move left and nothing there; try to page.
		handled = pageLeft();
	} else if (direction == FOCUS_RIGHT || direction == FOCUS_FORWARD) {
		// Trying to move right and nothing there; try to page.
		handled = pageRight();
	}
	if (handled) {
		playSoundEffect(SoundEffectConstants.getContantForFocusDirection(direction));
	}
	return handled;
}
 
Example #29
Source File: LazyViewPager.java    From AndroidBase with Apache License 2.0 5 votes vote down vote up
public boolean arrowScroll(int direction) {
    View currentFocused = findFocus();
    if (currentFocused == this) currentFocused = null;

    boolean handled = false;

    View nextFocused = FocusFinder.getInstance().findNextFocus(this, currentFocused,
            direction);
    if (nextFocused != null && nextFocused != currentFocused) {
        if (direction == View.FOCUS_LEFT) {
            // 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.
            if (currentFocused != null && nextFocused.getLeft() >= currentFocused.getLeft()) {
                handled = pageLeft();
            } else {
                handled = nextFocused.requestFocus();
            }
        } else if (direction == View.FOCUS_RIGHT) {
            // 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.
            if (currentFocused != null && nextFocused.getLeft() <= currentFocused.getLeft()) {
                handled = pageRight();
            } else {
                handled = nextFocused.requestFocus();
            }
        }
    } else if (direction == FOCUS_LEFT || direction == FOCUS_BACKWARD) {
        // Trying to move left and nothing there; try to page.
        handled = pageLeft();
    } else if (direction == FOCUS_RIGHT || direction == FOCUS_FORWARD) {
        // Trying to move right and nothing there; try to page.
        handled = pageRight();
    }
    if (handled) {
        playSoundEffect(SoundEffectConstants.getContantForFocusDirection(direction));
    }
    return handled;
}
 
Example #30
Source File: ScrollViewer.java    From AirFree-Client with GNU General Public License v3.0 5 votes vote down vote up
/**
 * You can call this function yourself to have the scroll view perform
 * scrolling from a key event, just as if the event had been dispatched to
 * it by the view hierarchy.
 *
 * @param event The key event to execute.
 * @return Return true if the event was handled, else false.
 */
public boolean executeKeyEvent(KeyEvent event) {
 mTempRect.setEmpty();
 if (!canScroll()) {
	 if (isFocused()) {
		 View currentFocused = findFocus();
		 if (currentFocused == this) currentFocused = null;
		 View nextFocused = FocusFinder.getInstance().findNextFocus(this, currentFocused, View.FOCUS_DOWN);
		 return nextFocused != null && nextFocused != this && nextFocused.requestFocus(View.FOCUS_DOWN);
	 }
	 return false;
 }
 boolean handled = false;
 if (event.getAction() == KeyEvent.ACTION_DOWN) {
	 switch (event.getKeyCode()) {
	 case KeyEvent.KEYCODE_DPAD_UP:
		 if (!event.isAltPressed()) {
			 handled = arrowScroll(View.FOCUS_UP, false);
		 } else {
			 handled = fullScroll(View.FOCUS_UP, false);
		 }
		 break;
	 case KeyEvent.KEYCODE_DPAD_DOWN:
		 if (!event.isAltPressed()) {
			 handled = arrowScroll(View.FOCUS_DOWN, false);
		 } else {
			 handled = fullScroll(View.FOCUS_DOWN, false);
		 }
		 break;
	 case KeyEvent.KEYCODE_DPAD_LEFT:
		 if (!event.isAltPressed()) {
			 handled = arrowScroll(View.FOCUS_LEFT, true);
		 } else {
			 handled = fullScroll(View.FOCUS_LEFT, true);
		 }
		 break;
	 case KeyEvent.KEYCODE_DPAD_RIGHT:
		 if (!event.isAltPressed()) {
			 handled = arrowScroll(View.FOCUS_RIGHT, true);
		 } else {
			 handled = fullScroll(View.FOCUS_RIGHT, true);
		 }
		 break;
	 }
 }
 return handled;
}