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-

  • Can a Voice action button be added next to edittext in android
  • Responsive website show on Web view out of mobile width(Not Responsive) other way in Chrome Looks Good
  • Android Attaching a file to GMAIL - Can't attach empty file
  • What regex can be used to filter out dalvikvm AND dalvikvm-heap messages from the logcat
  • libz.so.1: cannot open shared object file
  • Retrofit 2 and RxJava error handling operators
  • 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.

    Related posts:

    Change date string format in android
    USB Accessory API on Samsung Galaxy S2 Android
    What is “context click” in Android SDK 23
    Border for an Image view in Android?
    Bluetooth Low Energy startScan on Android 6.0 does not find devices
    FindBugs Android Gradle No classes configured error
  • How to set image button backgroundimage for different state?
  • Styling the SearchView Widget using support library v21
  • How do you undo floating mode for the logcat window in Android Studio
  • findFragmentByTag() returns null after perform a FragmentTransaction using replace() method
  • android dynamically add element from layout xml
  • Sending Data through Headphone jack in Android phones
  • 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.