我发现当使用方向键或轨迹球在我的应用程序中导航时,如果您向右移动并且 ListView 失去焦点,当 ListView 重新获得焦点时,另一个 child 获得焦点而不是上次获得焦点.我尝试使用 onFocusChange(...) 来保存哪个 child 拥有焦点,但看起来直到失去焦点之后才调用它,所以我永远无法捕获最后一个拥有焦点的 child 。有没有办法捕获谁有焦点,这样我就可以在 ListView 再次捕获焦点后调用 child 的 requestFocus() ?
不幸的是,我不能使用处理程序,因为这不是一个经常使用的功能,我不想为了较小的功能而牺牲性能。
这是我没有用的代码(无论如何 focusedView 总是 null):
mainListView.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if(focusedView == null ) { Log.i("focus", "focusedView == null"); }
if(hasFocus && focusedView != null) {
Log.i("focus", "Focus has been Recieved.............");
focusedView.requestFocus();
} else {
// Focus has been lost so save the id of what the user had selected
Log.i("focus", "Focus has been Lost.............");
focusedView = mainListView.findFocus();
}
}
});
谢谢!
最佳答案
我认为您的答案是正确的,因为焦点监听器在失去焦点后被调用,所以没有 child 会成为焦点,因此 .findFocus() 将返回 null。
我认为您最好的选择是扩展 ListView 类,并覆盖 onFocusChanged 方法。基本上做你正在做的事情,但是当 gainFocus 为真时在那里做。
@Override
protected void onFocusChanged (boolean gainFocus,
int direction, Rect previouslyFocusedRect) {
if(gainFocus)
focusedView = mainListView.findFocus();
}
关于android - 得到最后一个有焦点的 child ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6588807/