Search code examples
javaandroidscrollview

ScrollView: Pass touch events to children


I made a "joystick" out of an ImageView. It is inside a ScrollView.

It works without the ScrollView but inside the ScrollView it does not receive TouchEvents for vertical movement.

How can I stop the ScrollView from blocking the touch event?


Solution

  • I found a solution here.

    I created a custom ScrollView class that has a boolean indicating whether it is locked. The onInterceptTouchEvent method is overridden:

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        if (!mScrollable) return false;
        return super.onInterceptTouchEvent(ev);
    }
    

    When the joystick is touched (ACTION_DOWN), the custom ScrollView is locked. At the ATCTION_UP event it is unlocked.