Search code examples
androidscrollviewandroid-custom-viewtouch-event

Differentiate touch events between customView and scroll view


I have one customView and customView added in scroll view. Now i want differentiate both touch events. My problem is when try to scroll, customView also getting touch event and when i try to change in customView, scroll view getting events.

  1. How can we stop customView touch event when scrolling.
  2. How can we stop scroll touch events when customView wants events.

Thanks in advance


Solution

  • You can set touch listener to child view and then in onTouch() event, you can block intercept touch event of parent.

    You can use the same code as in https://stackoverflow.com/revisions/19311197/1

    v.setOnTouchListener(new OnTouchListener() {
        // Setting on Touch Listener for handling the touch inside ScrollView
        @Override
        public boolean onTouch(View v, MotionEvent event) {
        // Disallow the touch request for parent scroll on touch of child view
        v.getParent().requestDisallowInterceptTouchEvent(true);
        return false;
        }
    });
    

    About the second question, I don't know exactly what you're doing with customview but maybe you'd like to use click events instead because it's rather not user friendly to use different logic in ontouch and onclick as it will always fire up unexpectedly.