我有viewpager片段。当我尝试移动viewpager它工作正常。但如果我快速滑动它给我下面的例外。
03-25 11:01:44.901: E/AndroidRuntime(20880): FATAL EXCEPTION: main
03-25 11:01:44.901: E/AndroidRuntime(20880): Process: com.sph.wanbaoandroid, PID: 20880
03-25 11:01:44.901: E/AndroidRuntime(20880): java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
03-25 11:01:44.901: E/AndroidRuntime(20880): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
03-25 11:01:44.901: E/AndroidRuntime(20880): at java.util.ArrayList.get(ArrayList.java:308)
03-25 11:01:44.901: E/AndroidRuntime(20880): at android.support.v4.view.ViewPager.performDrag(ViewPager.java:2076)
03-25 11:01:44.901: E/AndroidRuntime(20880): at android.support.v4.view.ViewPager.onTouchEvent(ViewPager.java:1999)
03-25 11:01:44.901: E/AndroidRuntime(20880): at com.sph.wb.pdf.controller.ViewPagerWithTapGestreDetection.onTouchEvent(ViewPagerWithTapGestreDetection.java:42)
03-25 11:01:44.901: E/AndroidRuntime(20880): at android.view.View.dispatchTouchEvent(View.java:8281)
03-25 11:01:44.901: E/AndroidRuntime(20880): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2471)
03-25 11:01:44.901: E/AndroidRuntime(20880): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2194)
03-25 11:01:44.901: E/AndroidRuntime(20880): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2477)
03-25 11:01:44.901: E/AndroidRuntime(20880): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2209)
03-25 11:01:44.901: E/AndroidRuntime(20880): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2477)
03-25 11:01:44.901: E/AndroidRuntime(20880): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2209)
03-25 11:01:44.901: E/AndroidRuntime(20880): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2477)
03-25 11:01:44.901: E/AndroidRuntime(20880): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2209)
03-25 11:01:44.901: E/AndroidRuntime(20880): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2477)
03-25 11:01:44.901: E/AndroidRuntime(20880): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2209)
03-25 11:01:44.901: E/AndroidRuntime(20880): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2477)
03-25 11:01:44.901: E/AndroidRuntime(20880): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2209)
03-25 11:01:44.901: E/AndroidRuntime(20880): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2332)
03-25 11:01:44.901: E/AndroidRuntime(20880): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1616)
03-25 11:01:44.901: E/AndroidRuntime(20880): at android.app.Activity.dispatchTouchEvent(Activity.java:2665)
03-25 11:01:44.901: E/AndroidRuntime(20880): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2280)
03-25 11:01:44.901: E/AndroidRuntime(20880): at android.view.View.dispatchPointerEvent(View.java:8489)
03-25 11:01:44.901: E/AndroidRuntime(20880): at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4890)
03-25 11:01:44.901: E/AndroidRuntime(20880): at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4741)
03-25 11:01:44.901: E/AndroidRuntime(20880): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4295)
03-25 11:01:44.901: E/AndroidRuntime(20880): at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4349)
03-25 11:01:44.901: E/AndroidRuntime(20880): at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4318)
03-25 11:01:44.901: E/AndroidRuntime(20880): at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4429)
03-25 11:01:44.901: E/AndroidRuntime(20880): at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4326)
03-25 11:01:44.901: E/AndroidRuntime(20880): at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4486)
03-25 11:01:44.901: E/AndroidRuntime(20880): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4295)
03-25 11:01:44.901: E/AndroidRuntime(20880): at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4349)
03-25 11:01:44.901: E/AndroidRuntime(20880): at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4318)
03-25 11:01:44.901: E/AndroidRuntime(20880): at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4326)
03-25 11:01:44.901: E/AndroidRuntime(20880): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4295)
03-25 11:01:44.901: E/AndroidRuntime(20880): at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:6705)
03-25 11:01:44.901: E/AndroidRuntime(20880): at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:6584)
03-25 11:01:44.901: E/AndroidRuntime(20880): at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6555)
03-25 11:01:44.901: E/AndroidRuntime(20880): at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6520)
03-25 11:01:44.901: E/AndroidRuntime(20880): at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6785)
03-25 11:01:44.901: E/AndroidRuntime(20880): at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
03-25 11:01:44.901: E/AndroidRuntime(20880): at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method)
03-25 11:01:44.901: E/AndroidRuntime(20880): at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:176)
03-25 11:01:44.901: E/AndroidRuntime(20880): at android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:6758)
03-25 11:01:44.901: E/AndroidRuntime(20880): at android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:6804)
03-25 11:01:44.901: E/AndroidRuntime(20880): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:813)
03-25 11:01:44.901: E/AndroidRuntime(20880): at android.view.Choreographer.doCallbacks(Choreographer.java:613)
03-25 11:01:44.901: E/AndroidRuntime(20880): at android.view.Choreographer.doFrame(Choreographer.java:581)
03-25 11:01:44.901: E/AndroidRuntime(20880): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:799)
03-25 11:01:44.901: E/AndroidRuntime(20880): at android.os.Handler.handleCallback(Handler.java:733)
03-25 11:01:44.901: E/AndroidRuntime(20880): at android.os.Handler.dispatchMessage(Han
答案 0 :(得分:2)
扩展ViewPager并覆盖onTouchEvent
和onInterceptTouchEvent
以忽略当前项目和ViewPager的子计数均为0的情况。
@Override
public boolean onTouchEvent(MotionEvent ev) {
if (getCurrentItem() == 0 && getChildCount() == 0) {
return false;
}
return super.onTouchEvent(ev);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
if (getCurrentItem() == 0 && getChildCount() == 0) {
return false;
}
return super.onInterceptTouchEvent(ev);
}
答案 1 :(得分:0)
在此处获取完整类 SafeViewPager,基于以上答案:
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.viewpager.widget.ViewPager;
public class SafeViewPager extends ViewPager {
public SafeViewPager(@NonNull Context context) {
super(context);
}
public SafeViewPager(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean performClick() {
return super.performClick();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (getCurrentItem() <= 0 && getChildCount() <= 0) {
return false;
}
if (event.getAction() == MotionEvent.ACTION_UP) {
performClick();
}
return super.onTouchEvent(event);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
if (getCurrentItem() <= 0 && getChildCount() <= 0) {
return false;
}
return super.onInterceptTouchEvent(event);
}
}