过聚焦在GridView控件
我想实施聚焦下一个项目和Overscrolls在列表的最后一个GridView。
I'm trying to implement a GridView that Focuses the next Item and "Overscrolls at the End of a List.
例如
1 2 3
4 5 6
7 8 9
我要滚动1> 2> 3> 4> 5> 6> ... 只是按pressing正确的密钥。现在我只能滚动1> 2> 3,然后停止和我有向下键滚动。
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.
我已经尝试设置code中的focusViews(在我的ArrayList适配器,填充在GridView的 getView()
法)
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);
但是,这并不工作。我找到了布尔*滚动(INT方向)
在的grep code
但theese是包装本地,我不能覆盖它们。关于如何解决这个任何建议。我可以用另外的看法,得到相同的布局作为一个GridView?
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?
我还设置了 OnFocusChangeListener
来看看,没有反应发生了什么。
I also set a OnFocusChangeListener
to see what happens with no reaction.
编辑:
我只是说这我的 MainActivity
,但现在它似乎的onkeydown
只有被调用时,GridView控件不处理该KeyEvent(如果在一排的最后一个项目被选中)。
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);
}
编辑2:这是,使f *** ING集团愚蠢的,但工作这么该死的罚款:D
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);
}
我真的不希望发布此作为回答,我真的不希望有使用code,因为它是这样一个愚蠢的解决办法
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:帮助还是需要
好吧,我设法简单地用一块code的,这对我来说是pretty足够的解决方案。
Ok, I managed this with a simple piece of Code and it is a pretty enough solution for me.
我在GridView类派生,并已覆盖的方法:
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;
}
这个解决方案很高兴的事情是,它并不需要查看任何一组ID和所有的GridView的/适配器工作。
Nice thing about this solution is that it doesn't need any set IDs of the Views and works with all GridViews / Adapters.
我为什么需要这么长的很简单,就是无处不在的Android的重点是名称焦点
exept在这儿,它被称为选择
。这就是为什么我没有找到所需要的方法。谁能告诉我,为什么他们忘了该公约? :)
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? :)