Get child that last had focus
I have found that when using a d-pad or trackball to navigate in my app if you move to the right and the list view looses focus, when the list view regains focus, a different child is given focus than last had focus. I tried to use onFocusChange(...) to save which child had focus but it looks like this isn't called until after the focus is lost so I can never grab which child last had focus. Is there a way to grab who had focus so I can then call requestFocus() on the child once the list view grabs focus again?
Unfortunately I cannot use a handler because this isn't a much used feature and I don't want to sacrifice the performance for a smaller feature.
Here is the code I had that didn't work (focusedView was always null no matter what):
mainListView.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if(foc开发者_JAVA技巧usedView == 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();
}
}
});
Thanks!
I think you are spot on with your own answer, bascially since the focus listener is called after its lost focus, no child will be in focus and therefore .findFocus() will return null.
I recon you best bet is to extend the ListView class, and override the onFocusChanged method. Basically do what you are doing, but do it inthere when gainFocus is true.
@Override
protected void onFocusChanged (boolean gainFocus,
int direction, Rect previouslyFocusedRect) {
if(gainFocus)
focusedView = mainListView.findFocus();
}
This one
精彩评论