OnClickListener on Views inside a custom ScrollView

I’m havin a horizontal ScrollView inside a ViewPager. To prevent the ViewPager to be scrolled when the end of the ScrollView is reached I use this class as per hint on SO:

public class CustomScrollView extends HorizontalScrollView {

public CustomScrollView(Context p_context, AttributeSet p_attrs) {
    super(p_context, p_attrs);
}

@Override
public boolean onInterceptTouchEvent(MotionEvent p_event) {

    return true;
}

@Override
public boolean onTouchEvent(MotionEvent p_event) {


    if (p_event.getAction() == MotionEvent.ACTION_MOVE
            && getParent() != null) {
        getParent().requestDisallowInterceptTouchEvent(true);
    }

    return super.onTouchEvent(p_event);
}
}

The onInterCeptTouchEvent seems to consume any click to that View and everything inside it. When I put Views into that ScrollView, their OnClickListeners won’t be called.

  • Access to Google API - GoogleAccountCredential.usingOAuth2 vs GoogleAuthUtil.getToken()
  • Android POST request using HttpUrlConnection getting “already connected”
  • Android: How to get a custom attribute of an XML in Activity class
  • Difference between drawable and drawable-mdpi
  • Default marker for android google MAPVIEW?
  • Android Exception: Did you forget to call 'public void setup (LocalActivityManager activityGroup)'
  • When I let onInterceptTouchEvent return false, the OnClickListeners are called, but the ScrollView can’t be scrolled.

    How can I put clickable Views inside that ScrollView?

    EDIT: After implementing Rotem’s answer, the onClickListener works, but it doesn’t only fire on click events but also on others, like fling. How can this be prevented?

    Related posts:

    Trying to get the display size of an image in an ImageView
    Android (Java) Simple Send and receive with Server - Fast Setup Challenge
    Generate bitmap from HTML in Android
    Is there an addHeaderView equivalent for RecyclerView?
    WebView reloading when Fragment in ViewPager is retained form BackStack
    Test Activity onCreate Exception
  • Resource entry ic_launcher already defined
  • Green images when doing a JPEG encoding from YUV_420_888 using the new Android camera2 api
  • Remove scrollbar from ScrollView programmatically in Android
  • Have a disabled onClick?
  • Android illegalstatexception
  • How to display gradient effect from top to bottom in android
  • 3 Solutions collect form web for “OnClickListener on Views inside a custom ScrollView”

    try to call onTouchEvent inside the implementation of onInterceptTouchEvent and then return false.

    Well, minutes after starting a bounty I found out how it works:

    in onInterceptTouchEvent I

    return super.onInterceptTouchEvent(p_event);
    

    Here is complete solution using answers to this question.

    public class CustomHorizontalScrollView extends HorizontalScrollView {
    
            public CustomHorizontalScrollView(Context context) {
                    super(context);
    
            }
            public CustomHorizontalScrollView(Context context, AttributeSet attrs) {
                    super(context, attrs);
            }
    
            @Override
            public boolean onInterceptTouchEvent(MotionEvent ev) {
                    boolean result = super.onInterceptTouchEvent(ev);
                    if(onTouchEvent(ev)) {
                            return result;
                    } else {
                            return false;
                    }
            }
    
            @Override
            public boolean onTouchEvent(MotionEvent ev) {
                if (ev.getAction() == MotionEvent.ACTION_MOVE
                        && getParent() != null) {
                    getParent().requestDisallowInterceptTouchEvent(true);
                }
                    return super.onTouchEvent(ev);
            }
    
    }
    
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.