recyclerview焦点

荒凉一梦 提交于 2019-12-15 05:10:58
//防止焦点丢失
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);
    }

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!