码迷,mamicode.com
首页 > 移动开发 > 详细

Android - HorizontalScrollView(ViewPager) within ScrollView Touch Handling

时间:2014-11-04 10:58:40      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:android   style   blog   http   io   color   ar   sp   div   

1.

public class CustomScrollView extends ScrollView {
    private GestureDetector mGestureDetector;
    View.OnTouchListener mGestureListener;

    public CustomScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mGestureDetector = new GestureDetector(context, new YScrollDetector());
        setFadingEdgeLength(0);
    }

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

    // Return false if we‘re scrolling in the x direction  
    class YScrollDetector extends SimpleOnGestureListener {
        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {             
            return Math.abs(distanceY) > Math.abs(distanceX);
        }
    }
}

2.

public class VerticalScrollView extends ScrollView {
private float xDistance, yDistance, lastX, lastY;

public VerticalScrollView(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);
}
}

 

Via: http://stackoverflow.com/questions/2646028/android-horizontalscrollview-within-scrollview-touch-handling

 

Android - HorizontalScrollView(ViewPager) within ScrollView Touch Handling

标签:android   style   blog   http   io   color   ar   sp   div   

原文地址:http://www.cnblogs.com/veins/p/4072916.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!