我有以下类,它扩展了ViewPager,因此我可以禁用多点触摸滑动。我想在我的应用程序中使用多点触控功能进行其他功能。当我使用这个课时,它会神秘地崩溃。
public class SingleSwipeViewPager extends ViewPager
{
public SingleSwipeViewPager(final Context context)
{
super(context);
}
public SingleSwipeViewPager(final Context context, final AttributeSet attrs)
{
super(context, attrs);
}
@Override
public boolean onTouchEvent(final MotionEvent ev)
{
if (ev.getPointerCount() == 1)
{
return super.onTouchEvent(ev);
}
else
{
return true;
}
}
}
异常消息是" java.lang.IllegalArgumentException:pointerIndex超出范围",堆栈跟踪是......
01-09 21:33:12.256 25542-25542/com.example E/InputEventReceiver: Exception dispatching input event.
01-09 21:33:12.261 25542-25542/com.example E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example, PID: 25542
java.lang.IllegalArgumentException: pointerIndex out of range
at android.view.MotionEvent.nativeGetAxisValue(Native Method)
at android.view.MotionEvent.getX(MotionEvent.java:2014)
at android.support.v4.view.MotionEventCompatEclair.getX(MotionEventCompatEclair.java:32)
at android.support.v4.view.MotionEventCompat$EclairMotionEventVersionImpl.getX(MotionEventCompat.java:110)
at android.support.v4.view.MotionEventCompat.getX(MotionEventCompat.java:462)
at android.support.v4.view.ViewPager.onTouchEvent(ViewPager.java:2080)
at com.example.app.SingleSwipeViewPager.onTouchEvent(SingleSwipeViewPager.java:38)
at android.view.View.dispatchTouchEvent(View.java:8472)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2400)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2093)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2406)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2107)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2406)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2107)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2406)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2107)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2406)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2107)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2406)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2107)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2406)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2107)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2406)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2107)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2406)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2107)
at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2625)
at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1770)
at android.app.Activity.dispatchTouchEvent(Activity.java:2742)
at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:60)
at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:60)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2586)
at android.view.View.dispatchPointerEvent(View.java:8667)
at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4129)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3995)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3550)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3603)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3569)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3686)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3577)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3743)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3550)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3603)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3569)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3577)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3550)
at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5813)
at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5787)
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5758)
at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5903)
at android.view.InputEventReceiver.dispatchInputEvent(
在处理扩展ViewPager的滑动功能方面的任何帮助都将非常感激。
答案 0 :(得分:0)
如果您只想为 viewpager 禁用滑动,则可以使用此代码
public class CustomViewPager extends ViewPager{
public CustomViewPager(Context context){
super(context);
}
public CustomViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev){
return false;
}
@Override
public boolean onTouchEvent(MotionEvent ev){
return false;
}
}