0

I'm trying to implement a GridView that Focuses the next Item and "Overscrolls at the End of a List.

E.g.

1 2 3
4 5 6
7 8 9

I want to scroll 1 > 2 > 3 > 4 > 5 > 6 > ... just by pressing the right Key. Right now I can only Scroll 1 > 2 > 3 and then it stops and I have to scroll with the down Key.

I already tried to set the focusViews in code (In the getView() method of my ArrayList Adapter, that fills the GridView)

view.setId(position);
view.setNextFocusLeftId(position-1);
view.setNextFocusRightId(position+1);

But that doesn't work. I found the boolean *Scroll(int direction) Methods on grepcode

But theese are Package Local and I can't overwrite them. Any suggestions on how to solve this. Can I use another View and get the same Layout as a Gridview?

I also set a OnFocusChangeListener to see what happens with no reaction.

Edit: I just added this to my MainActivity, but now it seems to onKeyDown only get called when the GridView doesn't handle the KeyEvent (If the Last Item in a row is selected).

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    switch (keyCode) {
    case KeyEvent.KEYCODE_DPAD_LEFT:
        if (focusedView > 0) {
            mContainer.setSelection(--focusedView);
            Log.v("TEST", focusedView+"");
        }
        return true;
    case KeyEvent.KEYCODE_DPAD_RIGHT:
        if (focusedView < mAdapter.getCount() - 1) {
            mContainer.setSelection(++focusedView);
            Log.v("TEST", focusedView+"");
        }
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

Edit 2: This is so f***ing stupid but works so damn fine :D

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    switch (keyCode) {
    case KeyEvent.KEYCODE_DPAD_LEFT:
        mContainer.onKeyDown(KeyEvent.KEYCODE_DPAD_UP, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_UP));
        mContainer.onKeyDown(KeyEvent.KEYCODE_DPAD_RIGHT, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_RIGHT));
        mContainer.onKeyDown(KeyEvent.KEYCODE_DPAD_RIGHT, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_RIGHT));
        return true;
    case KeyEvent.KEYCODE_DPAD_RIGHT:
        mContainer.onKeyDown(KeyEvent.KEYCODE_DPAD_DOWN, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_DOWN));
        mContainer.onKeyDown(KeyEvent.KEYCODE_DPAD_LEFT, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_LEFT));
        mContainer.onKeyDown(KeyEvent.KEYCODE_DPAD_LEFT, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_LEFT));
        return true;
    }

    return super.onKeyDown(keyCode, event);
}

I really don't want to post this as Answer, and I really don't want to have to use this Code because it is such a stupid workaround

;TLDR: Help still needed

chuck258
  • 912
  • 7
  • 16

1 Answers1

0

Ok, I managed this with a simple piece of Code and it is a pretty enough solution for me.

I derived from the GridView class and have Overridden the Method:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    boolean keyHandled = false;
    if (this.getSelectedView() != null) {
        int focusedViewPosition = this.getSelectedItemPosition();
        switch (keyCode) {
        case KeyEvent.KEYCODE_DPAD_LEFT:
            if (focusedViewPosition > 0) {
                int prevItemPosition = focusedViewPosition - 1;
                this.setSelection(prevItemPosition);
                keyHandled = true;
            }
            break;
        case KeyEvent.KEYCODE_DPAD_RIGHT:
            if (focusedViewPosition < this.getChildCount() - 1) {
                int nextItemPosition = focusedViewPosition + 1;
                this.setSelection(nextItemPosition);
                keyHandled = true;
            }
            break;
        default:
            keyHandled = super.onKeyDown(keyCode, event);
        }
    } else {
        keyHandled = super.onKeyDown(keyCode, event);
    }
    return keyHandled;
}

Nice thing about this solution is that it doesn't need any set IDs of the Views and works with all GridViews / Adapters.

Why I needed so long is simply that everywhere in Android the Focus is name Focus exept here where it is called Selection. Thats why I didn't find the needed Methods. Can anyone tell me why they forgot about that convention? :)

chuck258
  • 912
  • 7
  • 16