Search code examples
androidscrollandroid-viewpagerhorizontal-scrollingvertical-scrolling

ViewPager scrolling issue in Android


I have a ViewPager with dynamic number of images in it. This ViewPager is added as a custom row to a table view. As this table view can have multiple dynamic custom rows, I have to add this table view in a scrollview for scrolling.

Now my question is, when I am scrolling horizontally to View Pager, it's not exactly horizontal scrolling, it's mixed with some vertical scrolling as well. So when a vertical scrolling is detected the events are passed to the scroll view; which makes the ViewPager reset to the initial position.

So how can I pass back the events to ViewPager or avoid scrollview catching vertical scroll events?

Note: I tried disabling scrollview to vertical scrolling but that didn't stop it from capturing the vertical scroll events.


Solution

  • I found the solution for my question and this is how I did it.

    I over ride the on touch event for my Viewpager in the following way

    myViewPager.setOnTouchListener(new OnTouchListener() {
    
                    public boolean onTouch(View v, MotionEvent event) {
    
                        if(event.getAction() == MotionEvent.ACTION_MOVE && myScrollView!=null){
                            myScrollView.requestDisallowInterceptTouchEvent(true);
                        }
                        return false;
                    }
                });
    

    Note: myScrollView is parent of myViewPager, as said in my question.