VerticalViewPager 嵌套 ScrollView 滑动冲突(极简)

VerticalViewPager 嵌套 ScrollView 滑动冲突(极简)_第1张图片
简单暴力

覆写两个方法,不再像个傻*一样去判断 MotionEvent

/**
 * Created by kalshen on 2018/4/20.
 * 傻*scrollView
 * 垂直viewpager和scrollview的滚动冲突解决
 */
public class FoolishScrollView extends ScrollView {

    private boolean isOverScroll = false;

    public FoolishScrollView(Context context) {
        super(context);
    }

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

    @Override
    protected void onOverScrolled(int scrollX, int scrollY, boolean clampedX, boolean clampedY) {
        isOverScroll = clampedY;
        super.onOverScrolled(scrollX, scrollY, clampedX, clampedY);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        if (!isOverScroll) { //通知ViewPager不要干扰自身的操作
            getParent().requestDisallowInterceptTouchEvent(true);
            return super.onTouchEvent(ev);
        } else {
            getParent().requestDisallowInterceptTouchEvent(false);
            return isOverScroll = false;//这里需要重置isOverScroll
        }
    }
}
顺便再给你一个网络修复版
public class MyScrollView extends ScrollView {
    public MyScrollView(Context context) {
        super(context);
    }

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

    public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private boolean mCanScroll = true;
    private float mDownY;

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mDownY = ev.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                int scrollY = getScrollY();
                //滑到顶部或底部
                if ((scrollY == 0 && mDownY <= ev.getY())
                        || (getChildAt(0).getMeasuredHeight() == (scrollY + getHeight()) && mDownY >= ev.getY())) {
                    mCanScroll = false;
                }
                break;
            case MotionEvent.ACTION_UP:
                mCanScroll = true;
                break;
            case MotionEvent.ACTION_CANCEL:
                mCanScroll = true;
                break;
        }
        if (mCanScroll) { //通知ViewPager不要干扰自身的操作
            getParent().requestDisallowInterceptTouchEvent(true);
            return super.onTouchEvent(ev);
        } else {
            getParent().requestDisallowInterceptTouchEvent(false);
            return false;
        }
    }
}

两个随便选一个用就可以了。

你可能感兴趣的:(VerticalViewPager 嵌套 ScrollView 滑动冲突(极简))