我创建了一个像this这样的轮播。如何使用箭头键控制轮播以满足上一个和下一个选定的项目?
答案 0 :(得分:0)
因为有人没有回答我的问题,我会回答。我决定将两个名为moveprevious和movenext的函数添加到类轮播中,它的效果非常好。
boolean movePrevious() {
if (mItemCount > 0 && mSelectedPosition > 0) {
scrollToChild(mSelectedPosition - mFirstPosition - 1);
return true;
} else {
return false;
}
}
boolean moveNext() {
if (mItemCount > 0 && mSelectedPosition < mItemCount - 1) {
scrollToChild(mSelectedPosition - mFirstPosition + 1);
return true;
}
else {
return false;
}
}
然后我用下面的代码调用这些函数。
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_DPAD_LEFT:
////if (movePrevious()) {
playSoundEffect(SoundEffectConstants.NAVIGATION_LEFT);
moveNext();
////}
return true;
case KeyEvent.KEYCODE_DPAD_RIGHT:
/////if (moveNext()) {
playSoundEffect(SoundEffectConstants.NAVIGATION_RIGHT);
movePrevious();
////}
return true;
case KeyEvent.KEYCODE_DPAD_CENTER:
case KeyEvent.KEYCODE_ENTER:
mReceivedInvokeKeyDown = true;
// fallthrough to default handling
}
return super.onKeyDown(keyCode, event);
}