我在ViewPager中实现了onPageChangeListener。当我将蓝牙键盘连接到设备并按下“Tab”键时,视图寻呼机将自动切换到下一个视图。
我想知道有什么方法可以禁用视图寻呼机的“Tab”键操作吗?我想要的是当我按下键盘上的“Tab”键时,视图寻呼机将不会切换到下一个视图。但是当我用手指滑动时,它仍会切换。
private ViewPager.OnPageChangeListener mPageChangeListener =
new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrollStateChanged(int state) {
if (mCurrentView != null) {
mIsScrolling = state != ViewPager.SCROLL_STATE_IDLE;
mCurrentView.setIsScrolling(mIsScrolling);
}
if (mCallback != null) {
mCallback.onScrollStateChanged(state);
}
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
if (mCurrentView != null) {
mCurrentView.setIsScrolling(false);
}
if (mCallback != null) {
mCallback.showView(position);
}
}
};
答案 0 :(得分:2)
在查看ViewPager的源代码之后,按照设计,当我们按下TAB时,它会对下一个视图执行arrowScroll(FOCUS_RIGHT)。我能想到的唯一方法是创建一个自定义viewpager,覆盖executeKeyEvent()函数。
public boolean executeKeyEvent(KeyEvent event) {
boolean handled = false;
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (event.getKeyCode()) {
case KeyEvent.KEYCODE_DPAD_LEFT:
handled = arrowScroll(FOCUS_LEFT);
break;
case KeyEvent.KEYCODE_DPAD_RIGHT:
handled = arrowScroll(FOCUS_RIGHT);
break;
case KeyEvent.KEYCODE_TAB:
if (KeyEventCompat.hasNoModifiers(event)) {
handled = arrowScroll(FOCUS_FORWARD);
} else if (KeyEventCompat.hasModifiers(event, KeyEvent.META_SHIFT_ON)) {
handled = arrowScroll(FOCUS_BACKWARD);
}
break;
}
}
return handled;
}