HorizontalScrollView within ScrollView Touch Handling

前端 未结 8 1968
轮回少年
轮回少年 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:53

    It wasn't working well for me. I changed it and now it works smoothly. If anyone interested.

    public class ScrollViewForNesting extends ScrollView {
        private final int DIRECTION_VERTICAL = 0;
        private final int DIRECTION_HORIZONTAL = 1;
        private final int DIRECTION_NO_VALUE = -1;
    
        private final int mTouchSlop;
        private int mGestureDirection;
    
        private float mDistanceX;
        private float mDistanceY;
        private float mLastX;
        private float mLastY;
    
        public ScrollViewForNesting(Context context, AttributeSet attrs,
                int defStyle) {
            super(context, attrs, defStyle);
    
            final ViewConfiguration configuration = ViewConfiguration.get(context);
            mTouchSlop = configuration.getScaledTouchSlop();
        }
    
        public ScrollViewForNesting(Context context, AttributeSet attrs) {
            this(context, attrs,0);
        }
    
        public ScrollViewForNesting(Context context) {
            this(context,null);
        }    
    
    
        @Override
        public boolean onInterceptTouchEvent(MotionEvent ev) {      
            switch (ev.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    mDistanceY = mDistanceX = 0f;
                    mLastX = ev.getX();
                    mLastY = ev.getY();
                    mGestureDirection = DIRECTION_NO_VALUE;
                    break;
                case MotionEvent.ACTION_MOVE:
                    final float curX = ev.getX();
                    final float curY = ev.getY();
                    mDistanceX += Math.abs(curX - mLastX);
                    mDistanceY += Math.abs(curY - mLastY);
                    mLastX = curX;
                    mLastY = curY;
                    break;
            }
    
            return super.onInterceptTouchEvent(ev) && shouldIntercept();
        }
    
    
        private boolean shouldIntercept(){
            if((mDistanceY > mTouchSlop || mDistanceX > mTouchSlop) && mGestureDirection == DIRECTION_NO_VALUE){
                if(Math.abs(mDistanceY) > Math.abs(mDistanceX)){
                    mGestureDirection = DIRECTION_VERTICAL;
                }
                else{
                    mGestureDirection = DIRECTION_HORIZONTAL;
                }
            }
    
            if(mGestureDirection == DIRECTION_VERTICAL){
                return true;
            }
            else{
                return false;
            }
        }
    }
    

提交回复
热议问题