11

So I have my activity which has a main ViewPager and inside of the ViewPager each page has the whole content as a ScrollView and inside of that ScrollView there is another ViewPager.

This might sound crazy but basically the outer ViewPager contains news articles, and the articles are long so there is a ScrollView, and inside the ScrollView there are multiple thumbnails/pictures that they can swipe through as well.

I've tried a few different custom ViewPagers with different touch event interception but can't seem to get it perfect. It either completely will absorb all touch events so that the vertical scrolling of the ScrollView doesn't work in that area, or it will be really touchy/difficult to get the inner one to scroll horizontally.

Anyone have a perfect solution?

egfconnor
  • 2,637
  • 1
  • 26
  • 44
  • Have you solved your problem? I am trying to do what exactly you tried. – tasomaniac Aug 29 '13 at 09:56
  • Yes my solution is below. Let me know if you need help! – egfconnor Sep 06 '13 at 18:13
  • I have tried that solution but it gives me problems with clickable items within the scroll layout. When I scroll vertically on a clickable item, the scroll view is not scrolling. – tasomaniac Sep 06 '13 at 18:30
  • Is that clickable item inside the inner view pager or in just the ScrollView? – egfconnor Sep 06 '13 at 18:50
  • Now that I think of it, nothing in my ScrollView is clickable besides what is inside of the inner ViewPager. Would guess the onInterceptTouchEvent() is messing that up for you... – egfconnor Sep 06 '13 at 18:51
  • 2
    I have exact same layout. The clickable items are in the ScrollView. There is a second way of doing the same thing you did that I found in stackoverflow. In that solution, they compute everything instead of using GestureDetector. I have used it and the problem is gone. Thank you. – tasomaniac Sep 07 '13 at 11:29
  • Could you please link to it for reference? – egfconnor Sep 09 '13 at 17:32
  • 1
    Sure. I am using the answer below the selected answer in the below link: http://stackoverflow.com/questions/2646028/android-horizontalscrollview-within-scrollview-touch-handling – tasomaniac Sep 10 '13 at 07:53

1 Answers1

12

In case anyone wants to know my solution:

public class CustomScrollView extends ScrollView {
private GestureDetector mGestureDetector;
View.OnTouchListener mGestureListener;

public CustomScrollView(Context context, AttributeSet attrs) {
    super(context, attrs);
    mGestureDetector = new GestureDetector(context, new YScrollDetector());
    setFadingEdgeLength(0);
}

@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 SimpleOnGestureListener {
    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2,
            float distanceX, float distanceY) {
        if (Math.abs(distanceY) > Math.abs(distanceX)) {
            return true;
        }
        return false;
    }
}
}

and the outer most ViewPager is:

public class NestingViewPager extends ViewPager {

public NestingViewPager(final Context context, final AttributeSet attrs) {
    super(context, attrs);
}

public NestingViewPager(final Context context) {
    super(context);
}

@Override
protected boolean canScroll(View v, boolean checkV, int dx, int x, int y) {
    if (v != this && v instanceof ViewPager) {
        return true;
    }
    return super.canScroll(v, checkV, dx, x, y);
}
}
egfconnor
  • 2,637
  • 1
  • 26
  • 44
  • Hey i have a problem with a scroll view and view pager inside of it but verticall scroll doesn't work even with ur custon scrollview ?? – Antwan Aug 14 '15 at 12:05
  • @egfconnor your solution is not working for me.. :( i cant scroll vertically over view pager. – Aks4125 Mar 03 '16 at 06:41
  • Hello, could you please elaborate a little bit more your answer? How do you tell your NestingViewPager to use the CustomScrollView instead of the normal ScrollView? Thank you – Loebre Dec 06 '16 at 11:28