@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_ENTER: //确定键enter
case KeyEvent.KEYCODE_DPAD_CENTER:
Log.d(TAG,"enter--->");
break;
case KeyEvent.KEYCODE_BACK: //返回键
Log.d(TAG,"back--->");
return true; //这里由于break会退出,所以我们自己要处理掉 不返回上一层
case KeyEvent.KEYCODE_SETTINGS: //设置键
Log.d(TAG,"setting--->");
break;
case KeyEvent.KEYCODE_DPAD_DOWN: //向下键
/* 实际开发中有时候会触发两次,所以要判断一下按下时触发 ,松开按键时不触发
* exp:KeyEvent.ACTION_UP
*/
if (event.getAction() == KeyEvent.ACTION_DOWN){
Log.d(TAG,"down--->");
}
break;
case KeyEvent.KEYCODE_DPAD_UP: //向上键
Log.d(TAG,"up--->");
break;
case KeyEvent.KEYCODE_0: //数字键0
Log.d(TAG,"0--->");
break;
case KeyEvent.KEYCODE_DPAD_LEFT: //向左键
Log.d(TAG,"left--->");
break;
case KeyEvent.KEYCODE_DPAD_RIGHT: //向右键
Log.d(TAG,"right--->");
break;
case KeyEvent.KEYCODE_INFO: //info键
Log.d(TAG,"info--->");
break;
case KeyEvent.KEYCODE_PAGE_DOWN: //向上翻页键
case KeyEvent.KEYCODE_MEDIA_NEXT:
Log.d(TAG,"page down--->");
break;
case KeyEvent.KEYCODE_PAGE_UP: //向下翻页键
case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
Log.d(TAG,"page up--->");
break;
case KeyEvent.KEYCODE_VOLUME_UP: //调大声音键
Log.d(TAG,"voice up--->");
break;
case KeyEvent.KEYCODE_VOLUME_DOWN: //降低声音键
Log.d(TAG,"voice down--->");
break;
case KeyEvent.KEYCODE_VOLUME_MUTE: //禁用声音
Log.d(TAG,"voice mute--->");
break;
break;
default:
break;
}
return super.onKeyDown(keyCode, event);
}
public class FocusGridLayoutManager extends GridLayoutManager {
public FocusGridLayoutManager(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
public FocusGridLayoutManager(Context context, int spanCount) {
super(context, spanCount);
}
public FocusGridLayoutManager(Context context, int spanCount, int orientation, boolean reverseLayout) {
super(context, spanCount, orientation, reverseLayout);
}
@Override
public int getChildCount() {
return super.getChildCount();
}
@Override
public View getChildAt(int index) {
return super.getChildAt(index);
}
@Override
public int getItemCount() {
return super.getItemCount();
}
@Override
public View getFocusedChild() {
return super.getFocusedChild();
}
@Override
public int getPosition(View view) {
return super.getPosition(view);
}
/**
* 获取列数
* @return
*/
@Override
public int getSpanCount() {
return super.getSpanCount();
}
/**
* 防止当recyclerview上下滚动的时候焦点乱跳
*/
@Override
public View onFocusSearchFailed(View focused, int focusDirection, RecyclerView.Recycler recycler, RecyclerView.State state) {
// Need to be called in order to layout new row/column
View nextFocus = super.onFocusSearchFailed(focused, focusDirection, recycler, state);
if (nextFocus == null) {
return null;
}
/**
* 获取当前焦点的位置
*/
int fromPos = getPosition(focused);
/**
* 获取我们希望的下一个焦点的位置
*/
int nextPos = getNextViewPos(fromPos, focusDirection);
return findViewByPosition(nextPos);
}
/**
* Manually detect next view to focus.
*
* @param fromPos from what position start to seek.
* @param direction in what direction start to seek. Your regular {@code View.FOCUS_*}.
* @return adapter position of next view to focus. May be equal to {@code fromPos}.
*/
protected int getNextViewPos(int fromPos, int direction) {
int offset = calcOffsetToNextView(direction);
if (hitBorder(fromPos, offset)) {
return fromPos;
}
return fromPos + offset;
}
/**
* 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;
}
/**
* Checks if we hit borders.
*
* @param from from what position.
* @param offset offset to new position.
* @return {@code true} if we hit border.
*/
private boolean hitBorder(int from, int offset) {
int spanCount = getSpanCount();
if (Math.abs(offset) == 1) {
int spanIndex = from % spanCount;
int newSpanIndex = spanIndex + offset;
return newSpanIndex < 0 || newSpanIndex >= spanCount;
} else {
int newPos = from + offset;
return newPos < 0 && newPos >= spanCount;
}
}
}