0

I've a layout in which Custom ScrollView has Horizontal viewpager and TextView below it. The onClick of TextView stops working if I use InterpretedScrollView instead of ScrollView. I need InterpretedScrollView to allow horizontal scroll in 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>

and my custom 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);
    }
}

Why is My InterceptedScrollview eating onClick of the TextView? How do I fix it?

Taranfx
  • 10,361
  • 17
  • 77
  • 95

1 Answers1

1

my custom ScrollView use totalSize of xScroll and yScroll. i record current xPoint and yPoint,when click down. calculate it when it moving .

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);
}
}
yaming
  • 68
  • 1
  • 1
  • 6