ViewPager inside a ScrollView does not scroll correclty ViewPager inside a ScrollView does not scroll correclty android android

ViewPager inside a ScrollView does not scroll correclty


I had the same problem. My solution was to call requestDisallowInterceptTouchEvent when the ViewPager scroll started.

Here is my code:

pager.setOnTouchListener(new View.OnTouchListener() {    @Override    public boolean onTouch(View v, MotionEvent event) {        v.getParent().requestDisallowInterceptTouchEvent(true);        return false;    }});pager.setOnPageChangeListener(new SimpleOnPageChangeListener() {    @Override    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {        pager.getParent().requestDisallowInterceptTouchEvent(true);    }});


Further reading has revealed that there are issues with scrolling components inside scrolling components.

One solution is to 'disable' the vertical scrolling of the ScrollView on the area of the contained scrollable component, in my case a ViewPager.

The code for this solution is detailed here (and its simply brilliant!)


Here's a solution:

    mPager.setOnTouchListener(new View.OnTouchListener() {        int dragthreshold = 30;        int downX;        int downY;        @Override        public boolean onTouch(View v, MotionEvent event) {            switch (event.getAction()) {            case MotionEvent.ACTION_DOWN:                downX = (int) event.getRawX();                downY = (int) event.getRawY();                break;            case MotionEvent.ACTION_MOVE:                int distanceX = Math.abs((int) event.getRawX() - downX);                int distanceY = Math.abs((int) event.getRawY() - downY);                if (distanceY > distanceX && distanceY > dragthreshold) {                    mPager.getParent().requestDisallowInterceptTouchEvent(false);                    mScrollView.getParent().requestDisallowInterceptTouchEvent(true);                } else if (distanceX > distanceY && distanceX > dragthreshold) {                    mPager.getParent().requestDisallowInterceptTouchEvent(true);                    mScrollView.getParent().requestDisallowInterceptTouchEvent(false);                }                break;            case MotionEvent.ACTION_UP:                mScrollView.getParent().requestDisallowInterceptTouchEvent(false);                mPager.getParent().requestDisallowInterceptTouchEvent(false);                break;            }            return false;        }    });

Basically setting the X,Y values when you press down, and calculating the distance while dragging to determine which way we'd like to go. Play around with the dragthreshold to optimize for your case.