【问题标题】:ViewPager with Swipe and clickable view insideViewPager 内部带有 Swipe 和可点击视图
【发布时间】:2018-04-23 10:17:42
【问题描述】:

我正在按照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中拦截触摸和滑动手势

【问题讨论】:

    标签: android android-viewpager


    【解决方案1】:

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

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

    你需要写

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-30
      • 2015-05-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多