//防止焦点丢失
public boolean onKeyDown(int keyCode, KeyEvent event) {
long current = System.currentTimeMillis();
boolean dispatch;
if (current - tempTime < 150) {
dispatch = true;
} else {
dispatch = super.onKeyDown(keyCode, event);
tempTime = current;
}
return dispatch;
}
//recyclerview tv焦点跳转指定位置
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {
if (recyclerView.getChildLayoutPosition(recyclerView.getChildAt(0)) == 0) {
recyclerView.scrollToPosition(adapter.getItemCount() - 1);
requestFocus(recyclerView, adapter.getItemCount() - 1);
} else {
// Toast.makeText(this, "haha", Toast.LENGTH_SHORT).show();
requestFocus(recyclerView, recyclerView.getChildLayoutPosition(recyclerView.getChildAt(0)));
}
}
if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
if (recyclerView.getChildLayoutPosition(recyclerView.getChildAt(0)) == adapter.getItemCount() - 1) {
recyclerView.scrollToPosition(0);
requestFocus(recyclerView, 0);
} else {
// Toast.makeText(this, "haha", Toast.LENGTH_SHORT).show();
requestFocus(recyclerView, recyclerView.getChildLayoutPosition(recyclerView.getChildAt(0)));
}
}
return super.onKeyDown(keyCode, event);
}
来源:CSDN
作者:技术小菜白某人
链接:https://blog.csdn.net/weixin_43528663/article/details/103470204