DImuthuUpe / AndroidPdfViewer

Android view for displaying PDFs rendered with PdfiumAndroid
Apache License 2.0
8.18k stars 1.91k forks source link

PDFView in a ScrollView #51

Open JeanChristophePal opened 8 years ago

JeanChristophePal commented 8 years ago

Hello,

I need to put the PDFView in a ScrollView, how should I do it?

So far, either the PDFView is not showing or the scroll is not working.

Thank you

JeanChristophePal commented 8 years ago

The problem is mainly due to the ScrollView behavior. To swipe horizontally a PDFView, which is in a Vertical Scrollview, motion has to be rigorously horizontal, if its a little UP or DOWN the swipe is stopped...

The hack found on internet is to test the motion distance (more vertically than horizontally?):

public class EnableLateralMotionScrollView extends ScrollView {

    private GestureDetector mGestureDetector;

    public EnableLateralMotionScrollView(Context context, AttributeSet attrs  ) {
        super(context, attrs);
        mGestureDetector = new GestureDetector(context, new YScrollDetector());
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return super.onInterceptTouchEvent(ev) && mGestureDetector.onTouchEvent(ev);
    }

    // Return false if we're scrolling in the x direction
    class YScrollDetector extends GestureDetector.SimpleOnGestureListener {
        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            return Math.abs(distanceY) > Math.abs(distanceX);
        }
    }

}

This is not perfect but it worked.

Do you have a better solution?

rickyngan commented 7 years ago

I find this another workable solution using ScrollView subclass (my case is in minSDKVersion API 16)

public class NoInterceptScrollView extends ScrollView {

    public NoInterceptScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return false;
    }
}

refer: https://stackoverflow.com/questions/17671123/scrollview-inside-a-scrollview-in-android-issue