Search code examples
androidhorizontalscrollview

Invert scrolling direction HorizontalScrollview


On default if you swipe from left to right you scroll to the left, and vice versa.

But I want to invert this action. so if you swipe left to right the scrollview goes to the right. Is there a XML solution or do I have to do this in code?


Solution

  • I am pretty unsure about this but something along these lines:

    1. Intercept touch event
    2. Invert the X positions
    3. Dispatch the new inverted touch event

    Code (not really):

    HorizontalScrollView hsv;
    hsv.setOnTouchListener(new OnTouchListener() {
    
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            event.setLocation(-1*event.getX(), event.getY());
            hsv.dispatchTouchEvent(ev);
            return true;
        }
    })