带有滑动和可单击的视图的ViewPager内部



我正在遵循从介质的教程创建垂直视图的教程,并且它可以很好地工作。以下是相同的代码:

public class CustomViewPager extends ViewPager {
public static final int HORIZONTAL = 0;
public static final int VERTICAL = 1;
private int mSwipeOrientation;
private ScrollerCustomDuration mScroller = null;
public CustomViewPager(Context context) {
    super(context);
    mSwipeOrientation = HORIZONTAL;
}
public CustomViewPager(Context context, AttributeSet attrs) {
    super(context, attrs);
    setSwipeOrientation(context, attrs);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
    if (mSwipeOrientation == VERTICAL) {
        boolean intercepted = super.onInterceptHoverEvent(swapXY(event));
        swapXY(event);
        return intercepted;
    }
    return super.onInterceptTouchEvent(event);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
    return super.onTouchEvent(mSwipeOrientation == VERTICAL ? swapXY(event) : event);
}
public void setSwipeOrientation(int swipeOrientation) {
    if (swipeOrientation == HORIZONTAL || swipeOrientation == VERTICAL)
        mSwipeOrientation = swipeOrientation;
    else
        throw new IllegalStateException("Swipe Orientation can be either CustomViewPager.HORIZONTAL" +
                " or CustomViewPager.VERTICAL");
    initSwipeMethods();
}
private void setSwipeOrientation(Context context, AttributeSet attrs) {
    TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomViewPager);
    mSwipeOrientation = typedArray.getInteger(R.styleable.CustomViewPager_swipe_orientation, 0);
    typedArray.recycle();
    initSwipeMethods();
}
private void initSwipeMethods() {
    if (mSwipeOrientation == VERTICAL) {
        // The majority of the work is done over here
        setPageTransformer(true, new VerticalPageTransformer());
        // The easiest way to get rid of the overscroll drawing that happens on the left and right
        setOverScrollMode(OVER_SCROLL_NEVER);
    }
}
/**
 * Set the factor by which the duration will change
 */
public void setScrollDurationFactor(double scrollFactor) {
    mScroller.setScrollDurationFactor(scrollFactor);
}
private MotionEvent swapXY(MotionEvent event) {
    float width = getWidth();
    float height = getHeight();
    float newX = (event.getY() / height) * width;
    float newY = (event.getX() / width) * height;
    event.setLocation(newX, newY);
    return event;
}
private class VerticalPageTransformer implements ViewPager.PageTransformer {
    @Override
    public void transformPage(@NonNull View page, float position) {
        if (position < -1) {
            // This page is way off-screen to the left
            page.setAlpha(0);
        } else if (position <= 1) {
            page.setAlpha(1);
            // Counteract the default slide transition
            page.setTranslationX(page.getWidth() * -position);
            // set Y position to swipe in from top
            float yPosition = position * page.getHeight();
            page.setTranslationY(yPosition);
        } else {
            // This page is way off screen to the right
            page.setAlpha(0);
        }
    }
}
@Override
protected boolean canScroll(final View v, final boolean checkV, final int dx, final int x, final int y) {
    if (v instanceof SubsamplingScaleImageView) {
        SubsamplingScaleImageView imageViewTouch = (SubsamplingScaleImageView)v;
        return true;
    } else {
        return super.canScroll(v, checkV, dx, x, y);
    }
}

当我将单击的图像视图视为仅在项目中的视图时,就会发生问题。如果视图可单击,我的滚动功能将无法工作。另外,我最近开始展示mopub广告,甚至使图像不可点击,我的视图将在显示广告时停止刷新,并且始终仅进行单击事件。

如何在ViewPager中拦截触摸和滑动手势

使它起作用。是在犯一个小错误,而不是此代码

  boolean intercepted = super.onInterceptHoverEvent(swapXY(event));

您需要写

  boolean intercepted = super.onInterceptTouchEvent(swapXY(event));

最新更新