Мне пришлось придумать способ изобразить позиции и детей для работы с dpad. Вот код, который я написал, чтобы справиться с этим:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState){
View v = inflater.inflate(R.layout.settings_tile, null);
final ImageView closeBtn = (ImageView) v.findViewById(R.id.settingsCloseButton);
closeBtn.setFocusable(true);
closeBtn.setOnClickListener(this);
closeBtn.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
v.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
return false;
}
if (event.getAction() == KeyEvent.ACTION_DOWN) {
return !(KeyEvent.KEYCODE_DPAD_DOWN == keyCode);
}
return false;
}
});
} else {
v.setOnKeyListener(null);
}
}
});
final ListView lv = (ListView) v.findViewById(android.R.id.list);
lv.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch(keyCode) {
case KeyEvent.KEYCODE_DPAD_DOWN:
return !(lv.getSelectedItemPosition() < lv.getCount() - 1);
case KeyEvent.KEYCODE_DPAD_UP:
if (lv.getSelectedItemPosition() == 1) {
closeBtn.requestFocus();
}
return lv.getSelectedItemPosition() == 1;
case KeyEvent.KEYCODE_DPAD_RIGHT:
return true;
}
return false;
}
return false;
}
});
consumeKeyEvent(v);
return v;
}
private void consumeKeyEvent(Object obj) {
if (obj instanceof ViewGroup) {
for (int i = 0; i < ((ViewGroup)obj).getChildCount(); i++) {
consumeKeyEvent(((ViewGroup)obj).getChildAt(i));
}
return;
}
((View)obj).setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
Log.i(DEBUG_TAG, "Event consumed, " + keyCode);
return false;
}
});
}