ViewPager with WebView allowing swiping and disallowing scrolling of WebView

I am using ViewPager and WebViewto present HTML contents as an eBook. On some pages of the ViewPager I am allowing the TouchEvent to be handled by the WebView and on some pages I am letting the ViewPager handle the TouchEvent by itself by overriding the onInterceptTouchEvent method in the ViewPager(some pages are having TextViews and Buttons without WebView 1st and last page here). Here is code for the same-

@Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        if (getCurrentItem() == 0 || getCurrentItem() == getAdapter().getCount() - 1)
            return false;
        else {
            if (ev.getAction() == MotionEvent.ACTION_DOWN) {
                return false;
            }
            return true;
        }
    }

I am again disallowing the scrolling by setting OnTouchListener on the WebView using the following code-

  • android: How can I implement first time tutorial like Go Launcher in my app?
  • Android - margins specified in custom style not taking effect
  • H2 Database vs SQLite on Android
  • Android 3.0 - what are the advantages of using LoaderManager instances exactly?
  • Application class lifecycle while service is running
  • Adb Devices can't find my phone
  • contentView.setOnTouchListener((v, event) -> {
                if (MotionEventCompat.getActionMasked(event) == MotionEvent.ACTION_MOVE) {
                    ((BigBeeViewPager) v.getParent().getParent()).onInterceptTouchEvent(event);
                }
                return true;
            });
    

    Problem I am facing is – When I am trying to swipe the ViewPager from the middle of the pages it doesn’t work. I have to start swiping from the end of the pages. However it works fine for the pages which are not having WebView
    How to allow it to start swiping from the anywhere in the pages.

    This behaviour is similar to Gmail Android App where ViewPager is containing the WebView and Swiping is available from any where. I am not able to figure out how they have done it.

    Note – When I am intercepting all TouchEvent to the WebView it disables the long click and text selection too, which is not desirable.

    Thanks in advance for the help.

  • Populate Android Database From CSV file?
  • Android gradle build: running assembleDebug makes release tasks of project dependencies being called
  • Is there a built in API for handling pinch-zoom functionality in Android
  • Firebase Crash Reporting error: client application com.xxxx are blocked
  • google maps api v2 out of memory error
  • Android - Highlight a Word In a TextView?
  • 3 Solutions collect form web for “ViewPager with WebView allowing swiping and disallowing scrolling of WebView”

    Here you go man – an old problem with a nice solution.

    Take a look here Scroll webview horizontally inside a ViewPager

    You have create a GestureDetector and implement that on a Webviewlike this:

    GestureDetector gestureDetector=new GestureDetector(new GestureDetector.OnGestureListener() {
            @Override
            public boolean onDown(MotionEvent motionEvent) {
                return false;
            }
    
            @Override
            public void onShowPress(MotionEvent motionEvent) {
            }
    
            @Override
            public boolean onSingleTapUp(MotionEvent motionEvent) {
                return false;
            }
    
            @Override
            public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
                return false;
            }
    
            @Override
            public void onLongPress(MotionEvent motionEvent) {
            }
    
            @Override
            public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
                return false;
            }
        });
    
              webView.setOnTouchListener(new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        switch (event.getAction()) {
                            case MotionEvent.ACTION_DOWN:
                            case MotionEvent.ACTION_UP:
                                if (!v.hasFocus()) {
                                    v.requestFocus();
                                }
                                break;
                        }
                        gestureDetector.onTouchEvent(event);
                        return false;
                    }
                });
    

    And remove the Interceptor from Viewpager and use the default one without any changes

    However MarkySmarky is extremely helpful to find out the solution but still I will like to write the the answer part so other people can get help.

    I solved the problem by overriding the method

    protected boolean canScroll(View v, boolean checkV, int dx, int x, int y)
    

    of ViewPager and returning false if v is the instance of the WebView. This is not letting the WebView scroll itself and hence ViewPager treat it as a nono scrolling child and hence swiping works from the anywhere of the page.

    Finally my ViewPager is looking like-

    public class BigBeeViewPager extends ViewPager {
    
        public BigBeeViewPager(Context context) {
            super(context);
        }
    
        public BigBeeViewPager(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        @Override
        protected boolean canScroll(View v, boolean checkV, int dx, int x, int y) {
            return !(v instanceof ObservableWebView) && super.canScroll(v, checkV, dx, x, y);
        }
    }
    
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.