Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enable Scale in Android ScrollView in a way that doesn't prevent scrolling it and clicking on it's child items?

I have a custom view that extends Android ScrollView. The direct child is a relative layout which has children that are clickable. I want to be able to:

  1. detect onScale Gesture on the scroll view (than I will manually manage the scale of the items).
  2. scroll the ScrollView vertically.
  3. keep those child items clickable.

What I have tried so far is (pseudo code):

public class CustomView extends ScrollView { 
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {  
        return super.onInterceptTouchEvent(ev) || mScaleDetector.onTouchEvent(ev);
    }

   @Override
   public boolean onTouchEvent(MotionEvent ev) {
       return mScaleDetector.onTouchEvent(ev);
   }

    private class ScaleListener extends
        ScaleGestureDetector.SimpleOnScaleGestureListener {

            @Override
            public boolean onScale(ScaleGestureDetector detector) {
                // Handle the scale..
                return true;
            }
       }
}

I also tried different configurations for the onInterceptMethod such as first call the super and the return the mScaleDetector.onTouchEvent and so on.

I succeeded to intercept the scale or the click and scroll but not both.

Thanks, Daniel

like image 788
Daniel L. Avatar asked Dec 30 '12 15:12

Daniel L.


1 Answers

The solution is to use:

     @Override
    public boolean dispatchTouchEvent(MotionEvent ev){
        super.dispatchTouchEvent(ev);    
        return mScaleDetector.onTouchEvent(ev); 
    }

and not override onInterceptTouchEvent and onTouchEvent methods.

like image 185
Daniel L. Avatar answered Oct 05 '22 23:10

Daniel L.