首页 > 代码库 > ScrollView嵌套Viewpager和ListView的整合

ScrollView嵌套Viewpager和ListView的整合

1、ScrollView嵌套Viewpager解决滑动冲突:

(1)重新ScrollView(法一)

/**
 * 能够兼容ViewPager的ScrollView
 * @Description: 解决了ViewPager在ScrollView中的滑动反弹问题
 */
public class ScrollViewExtend extends ScrollView {
    // 滑动距离及坐标
    private float xDistance, yDistance, xLast, yLast;
    public ScrollViewExtend(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    @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);
    }
}

(2)重新ScrollView(法二)

public class MyScrollView extends ScrollView {

    private boolean canScroll;

 

    private GestureDetector mGestureDetector;

    View.OnTouchListener mGestureListener;

 

    public MyScrollView(Context context, AttributeSet attrs) {

        super(context, attrs);

        mGestureDetector = new GestureDetector(new YScrollDetector());

        canScroll = true;

    }

    @Override

    public boolean onInterceptTouchEvent(MotionEvent ev) {

        if(ev.getAction() == MotionEvent.ACTION_UP)

            canScroll = true;

        return super.onInterceptTouchEvent(ev) && mGestureDetector.onTouchEvent(ev);

    }

 

    class YScrollDetector extends SimpleOnGestureListener {

        @Override

        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {

            if(canScroll)

                if (Math.abs(distanceY) >= Math.abs(distanceX))

                    canScroll = true;

                else

                    canScroll = false;

            return canScroll;

        }

    }

}

  个人是用第一个方法解决的,根据不同的情况可以自己处理!


(2)ScrollView嵌套ListView请查看此处


  注:本文仅代码都是网上找的,本文仅作记录而已!

本文出自 “吸博取精自我更新” 博客,请务必保留此出处http://wyong.blog.51cto.com/1115465/1600295

ScrollView嵌套Viewpager和ListView的整合