Android ListView上下滑动与item左右滑动冲突解决

ListView添加了一个HeaderView广告位的轮播图,发现滑动不灵活了,猜想肯定是item左右滑动与ListView上下滑动的冲突,解决办法如下,自定义ListView,重写onInterceptTouchEvent方法,完美解决。

private float xDistance, yDistance, xLast, yLast;
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        // TODO Auto-generated method stub
        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)