AndroidPdfViewer icon indicating copy to clipboard operation
AndroidPdfViewer copied to clipboard

PDFView in a ScrollView

Open JeanChristophePal opened this issue 9 years ago • 3 comments

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 avatar Jul 29 '16 09:07 JeanChristophePal

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?

JeanChristophePal avatar Aug 01 '16 09:08 JeanChristophePal

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

rickyngan avatar Aug 27 '17 07:08 rickyngan

I have solve it using this

 private fun adjustPdfViewHeight(numberOfPages: Int) {
        binding.pdfView.viewTreeObserver.addOnGlobalLayoutListener(object :
            ViewTreeObserver.OnGlobalLayoutListener {
            override fun onGlobalLayout() {
                binding.pdfView.viewTreeObserver.removeOnGlobalLayoutListener(this)

                val pageWidth = binding.pdfView.measuredWidth
                var totalHeight = 0

                // Loop through each page to calculate its height
                for (i in 0 until numberOfPages) {
                    val pageSize = binding.pdfView.getPageSize(i)
                    val pageHeightRatio = pageSize.height / pageSize.width
                    val pageHeight = (pageWidth * pageHeightRatio).toInt()

                    totalHeight += pageHeight
                }

                val layoutParams = binding.pdfView.layoutParams as LinearLayout.LayoutParams
                layoutParams.height = totalHeight
                binding.pdfView.layoutParams = layoutParams
            }
        })
    }

bhavinatharva avatar Feb 07 '25 11:02 bhavinatharva