Overfocus in GridView

Posted by chuck258 on Stack Overflow See other posts from Stack Overflow or by chuck258
Published on 2013-11-05T14:09:12Z Indexed on 2013/11/05 15:53 UTC
Read the original article Hit count: 167

Filed under:
|

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

© Stack Overflow or respective owner

Related posts about android

Related posts about gridview