HorizontalScrollView within ScrollView Touch Handling

前端 未结 8 1792
轮回少年
轮回少年 2020-11-22 05:28

I have a ScrollView that surrounds my entire layout so that the entire screen is scrollable. The first element I have in this ScrollView is a HorizontalScrollView block tha

8条回答
  •  囚心锁ツ
    2020-11-22 05:57

    Thank you Joel for giving me a clue on how to resolve this problem.

    I have simplified the code(without need for a GestureDetector) to achieve the same effect:

    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);
        }
    }
    

提交回复
热议问题