0

I am using a ViewPager with a TouchImageView inside it and it works great, (I have used this solution in many of my Android apps). However I have an app for which there are many other controls on the same screen so they are all inside a scrollview control. In this scenario I see the scrollview does not play nice and I am not able to pan within the zoomed image. When I use my finger to pan upward or downward the entire page scrolls instead of the image panning.

So here is what I am trying to do.... Inside the TouchImageView I detect Zoom Begin and Zoom End and have created an interface to make a callback to my Activity onZoomBegin() and onZoomEnd() methods. In the onZoomBegin() method I want to disable the scrollview from responding to any touch events and in onZoomEnd() I can re-enable it. So far here are the things I have tried doing in the onZoomBegin() method for which none are working....

scrollView.setEnabled(false);
scrollView.requestDisallowInterceptTouchEvent(true);

also I have tried the answer to a similar question which was to takeover the onTouchListener like such:

 scrollView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            return true;
        }
    });

This does stop the scrollview from scrolling but the scrollview is still intercepting the touch events cause the image still will not pan up or down.

I've tried checking nestedScrollingEnabled in the layout designer, no joy.... I just want to know is there a way to totally disable a scrollview and then re-enable it from responding to touch events?

Wayne Fulcher
  • 741
  • 1
  • 11
  • 21

1 Answers1

0

I found this answer on another question somewhere but by the time I realized it was the solution to my problem (answer to my question) then I lost reference to it. I will keep looking so I can edit this post to give credit where credit is due.

public class CustomScrollView extends ScrollView {

// true if we can scroll the ScrollView
// false if we cannot scroll
private boolean scrollable = true;

public CustomScrollView(Context context) {
    super(context);
}

public CustomScrollView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public CustomScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
}


public void setScrollingEnabled(boolean scrollable) {
    this.scrollable = scrollable;
}

public boolean isScrollable() {
    return scrollable;
}

@Override
public boolean onTouchEvent(MotionEvent ev) {
    switch (ev.getAction()) {
        case MotionEvent.ACTION_DOWN:
            // if we can scroll pass the event to the superclass
            if (scrollable)
                return super.onTouchEvent(ev);
            // only continue to handle the touch event if scrolling enabled
            return false; // scrollable is always false at this point
        default:
            return super.onTouchEvent(ev);
    }
}

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    // Don't do anything with intercepted touch events if
    // we are not scrollable
    if (!scrollable)
        return false;
    else
        return super.onInterceptTouchEvent(ev);
}

}

This part I just figured out for myself.... In the TouchImageView I added a callback interface which is called when a zoom begins and ends so in my Activity I only had to do this:

    private class OnZoomListener implements TouchImageView.OnZoomListener {
    @Override
    public void onZoomBegin() {
        isZoomed = true;
        scrollView.scrollTo(0, 0);
        scrollView.setScrollingEnabled(false); // <-- disables scrollview
        hideImageControls();
        sizeViewPager();
    }

    @Override
    public void onZoomEnd() {
        scrollView.setScrollingEnabled(true); // <-- enables scrollview
        showImageControls();
        isZoomed = false;
    }
}
Wayne Fulcher
  • 741
  • 1
  • 11
  • 21
  • can you post your TouchImageView.java class? Because I am facing same problem. –  Feb 17 '17 at 11:15