使用D-PAD时列出视图焦点 [英] List view Focus while using D-PAD
问题描述
在我的图像中有一个侧边栏和ListView,我使用D-PAD访问每个Button. 当侧栏搜索按钮具有焦点并且我按向右D-PAD按钮时,我希望焦点移到ListView的第一项.但是,就我而言,它转到第二项.如果向下箭头具有焦点,并且我按向右按钮,它将转到第4个列表项.因此,我也需要防止这种情况.如何将焦点移至第一个?
In my Image there is a sidebar and ListView there, I access every Button by using D-PAD. When side bar search button has focus and I press right D-PAD button, I want the focus to go to the ListView's first item. But, in my case it goes to the second item. If Down arrow has focus and i press right button it goes to 4th list item.So , i need to prevent this also.How to move the focus to the 1st one?
if (Search != null && Search.hasFocus()) {
// Search.setNextFocusRightId(R.id.listView);
// listView.smoothScrollToPosition(0);
listView.setChoiceMode(1);
listView.setItemChecked(0, true);
}
推荐答案
最好的方法是在Activity类中重写dispatchKeyevent,然后将焦点/选择设置为Listview中您想要的任何位置.
The best thing to do is Override the dispatchKeyevent in your Activity class then set the focus/selection to the place wherever you like in Listview .
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
boolean handled = super.dispatchKeyEvent(event);
if(!handled && event.getAction() == KeyEvent.ACTION_UP)
{
if(yourParentViewGroup.getFocusedChild() is your SearchView && event.getKeyCode() == KeyEvent.KEYCODE_DPAD_RIGHT )
{
yourListvIew.setSelction(0);
handled = true;
}
}
return handled;
但是如果要处理Focus或选择逻辑,请记住返回"true",否则它将调用android focusfinder算法,并且焦点/选择将移至最近的View.
But remember to return "true" if you are going to handle the Focus or selection logic else it will invoke android focusfinder algorithm and focus/selection will shift to the nearest View.
这篇关于使用D-PAD时列出视图焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!