RecyclerView的换行问题,重写RecyclerView 的dispatchKeyEvent方法,在收到right和left事件中处理
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
boolean result = super.dispatchKeyEvent(event);
View focusView = this.getFocusedChild();
if (focusView == null) {
return result;
} else {
if (event.getAction() == KeyEvent.ACTION_UP) {
return true;
} else {
switch (event.getKeyCode()) {
case KeyEvent.KEYCODE_DPAD_RIGHT: {
View rightView = FocusFinder.getInstance().findNextFocus(this, focusView, View.FOCUS_RIGHT);
int id = this.getChildPosition(focusView);
if ((id + 1) == this.getChildCount()) {
return true;
}
if (rightView != null) {
rightView.requestFocus();
return true;
} else if ((id + 1) < this.getChildCount()) {
View nestView = this.getChildAt(id + 1);
if (nestView != null) {
nestView.requestFocus();
return true;
}
}
return false;
}
case KeyEvent.KEYCODE_DPAD_LEFT: {
View leftView = FocusFinder.getInstance().findNextFocus(this, focusView, View.FOCUS_LEFT);
Log.i(TAG, "leftView is null:" + (leftView == null));
int ids = this.getChildPosition(focusView);
if (ids == 0) {
return true;
}
if (leftView != null) {
leftView.requestFocus();
return true;
} else if ((ids - 1) > 0) {
View nestView = this.getChildAt(ids - 1);
if (nestView != null) {
nestView.requestFocus();
return true;
}
}
return false;
}
}
}
}
return super.dispatchKeyEvent(event);
}