Android ViewPager with ScrollViews with ViewPagers inside the ScrollViews

前端 未结 1 797
孤独总比滥情好
孤独总比滥情好 2020-12-29 11:21

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 V

相关标签:
1条回答
  • 2020-12-29 11:46

    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);
    }
    }
    
    0 讨论(0)
提交回复
热议问题