竖向ScrollView嵌套横向滑动布局冲突

当外层竖向滑动ScrollView里面嵌套横向的滑动布局时,会发先内层横向滑动很卡顿

原因:左右滑动操作被外层的scrollView处理掉了

解决:只要让scrollview对左右滑动事件不监听,让其子控件处理左右滑动事件 即可,需要重写scrollview的onInterceptTouchEvent方法,如下:

public class VerticalScrollView extends ScrollView {
    private float xDistance, yDistance, xLast, yLast;

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

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

    public VerticalScrollView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                xDistance = yDistance = 0f;
                xLast = ev.getX();
                yLast = ev.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                final float curX = ev.getX();
                final float curY = ev.getY();

                xDistance += Math.abs(curX - xLast);
                yDistance += Math.abs(curY - yLast);
                xLast = curX;
                yLast = curY;

                if (xDistance > yDistance) {
                    return false;
                }
        }
        return super.onInterceptTouchEvent(ev);
    }
}

你可能感兴趣的:(Android开发)