0

我有一个布局,其中 Custom ScrollView 在其下方有 Horizo​​ntal viewpager 和 TextView。如果我使用 InterpretedScrollView 而不是 ScrollView,则 TextView 的 onClick 将停止工作。我需要 InterpretedScrollView 以允许在 ViewPager 中水平滚动。

<com.package.InterceptedScrollView
   <ViewPager
    // scrolls horizontally thanks to InterceptedScrollView
     />
   ...
   <TextView
      // try scrolling vertically by touching this imageView and it doesn't scroll
      onClick="something"/>
</com.package.InterceptedScrollView>

和我的自定义 ScrollView

public class InterceptedScrollView extends ScrollView {
    GestureDetector mGestureDetector;

    private class VerticalScrollDetector extends SimpleOnGestureListener {
        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float x, float y) {
            if (Math.abs(y) > Math.abs(x)) {
                // scrolling in Y direction essentially. Should scroll
                // vertically.
                return true;
            }
            // Not much of scroll in Y. Don't scroll.
            return false;
        }
    }

    public InterceptedScrollView(Context ctx, AttributeSet as) {
        super(ctx, as);
        mGestureDetector = new GestureDetector(ctx, new VerticalScrollDetector());
        setFadingEdgeLength(0);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        return super.onInterceptTouchEvent(event) && mGestureDetector.onTouchEvent(event);
    }
}

为什么我的 InterceptedScrollview 会吃掉 TextView 的 onClick?我如何解决它?

4

1 回答 1

1

我的自定义 ScrollView 使用 xScroll 和 yScroll 的 totalSize。单击向下时,我记录当前的 xPoint 和 yPoint。当它移动时计算它。

public class PageScrollView extends ScrollView {

    private float xDistance, yDistance, lastX, lastY;

public PageScrollView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {

    switch (ev.getAction()) {
        case MotionEvent.ACTION_DOWN:
            xDistance = yDistance = 0f;
            lastX = ev.getX();
            lastY = ev.getY();
            break;
        case MotionEvent.ACTION_MOVE:
            final float curX = ev.getX();
            final float curY = ev.getY();
            xDistance += Math.abs(curX - lastX);
            yDistance += Math.abs(curY - lastY);
            lastX = curX;
            lastY = curY;
            if(xDistance > yDistance)
                return false;
    }

    return super.onInterceptTouchEvent(ev);
}
}
于 2013-09-05T02:03:49.633 回答