Convert KeyEvent DPad

84 Views Asked by At

I have a RecyclerView that is placed vertically. When an item inside the RecyclerView is focused, I can press the Left DPad button or the Up button to move the focus to the previous item, which moves the focus upwards on account of the RecyclerView being vertical. The problem is when I use the Left button, the focus goes to an element that is on the left (obviously). I want the two buttons to do the same thing when I'm in that RecyclerView. I thought of intercepting the Key event so KeyEvent.KEYCODE_DPAD_LEFT becomes KeyEvent.KEYCODE_DPAD_UP to solve the problem, but something goes wrong. Here's my code inside the Adapter Class: (the comments are the attempts I made)

public MyViewHolder(@NonNull View itemView) {
    super(itemView);
    item_of_rw = itemView.findViewById(R.id.item_of_rw);

    item_of_rw.setOnKeyListener(new View.OnKeyListener() {

        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT && otherControl) {
                KeyEvent convertedEvent = new KeyEvent(event.getAction(), KeyEvent.KEYCODE_DPAD_UP);
                v.dispatchKeyEvent(convertedEvent);
                // v.onKeyDown(KeyEvent.KEYCODE_DPAD_UP, convertedEvent);
                // v.dispatchKeyEventPreIme(new KeyEvent(KeyEvent.KEYCODE_DPAD_UP, KeyEvent.KEYCODE_DPAD_UP));
                // item_of_rw.dispatchKeyEvent(convertedEvent);
                // v.setNextFocusLeftId(v.getNextFocusUpId());
                return true;
            }
            return false;
        }
    });
}
0

There are 0 best solutions below