下面列出了android.view.FocusFinder#getInstance ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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);
}
@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);
}
/**
* 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();
}
}