ViewPager里面有滑动和可点击的视图

时间:2018-04-23 10:17:43

标签: android android-viewpager

我正在按照medium的教程创建垂直视图寻呼机,它运行良好。以下是相同的代码:

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中的触摸和滑动手势

1 个答案:

答案 0 :(得分:0)

搞定了。是犯了一个小错误,而不是这个代码

[root@06284cc1ae0c frozen]# ./app
this is from app

你需要写

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