HorizontalScrollView within ScrollView Touch Handling

前端 未结 8 1760
轮回少年
轮回少年 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 06:00

    Thanks to Neevek his answer worked for me but it doesn't lock the vertical scrolling when user has started scrolling the horizontal view(ViewPager) in horizontal direction and then without lifting the finger scroll vertically it starts to scroll the underlying container view(ScrollView). I fixed it by making a slight change in Neevak's code:

    private float xDistance, yDistance, lastX, lastY;
    
    int lastEvent=-1;
    
    boolean isLastEventIntercepted=false;
    @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(isLastEventIntercepted && lastEvent== MotionEvent.ACTION_MOVE){
                    return false;
                }
    
                if(xDistance > yDistance )
                    {
    
                    isLastEventIntercepted=true;
                    lastEvent = MotionEvent.ACTION_MOVE;
                    return false;
                    }
    
    
        }
    
        lastEvent=ev.getAction();
    
        isLastEventIntercepted=false;
        return super.onInterceptTouchEvent(ev);
    
    }
    
    0 讨论(0)
  • 2020-11-22 06:05

    I think I found a simpler solution, only this uses a subclass of ViewPager instead of (its parent) ScrollView.

    UPDATE 2013-07-16: I added an override for onTouchEvent as well. It could possibly help with the issues mentioned in the comments, although YMMV.

    public class UninterceptableViewPager extends ViewPager {
    
        public UninterceptableViewPager(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        @Override
        public boolean onInterceptTouchEvent(MotionEvent ev) {
            boolean ret = super.onInterceptTouchEvent(ev);
            if (ret)
                getParent().requestDisallowInterceptTouchEvent(true);
            return ret;
        }
    
        @Override
        public boolean onTouchEvent(MotionEvent ev) {
            boolean ret = super.onTouchEvent(ev);
            if (ret)
                getParent().requestDisallowInterceptTouchEvent(true);
            return ret;
        }
    }
    

    This is similar to the technique used in android.widget.Gallery's onScroll(). It is further explained by the Google I/O 2013 presentation Writing Custom Views for Android.

    Update 2013-12-10: A similar approach is also described in a post from Kirill Grouchnikov about the (then) Android Market app.

    0 讨论(0)
提交回复
热议问题