ScrollView中嵌入ViewPager,ViewPager的滑动出现问题

来源:互联网 发布:软件开发管理方法 编辑:程序博客网 时间:2024/05/18 03:16

在项目中遇到过这样一个问题,当ScrollView嵌套ViewPager时,Viewpager的滑动会受到影响,影响使用,经过多方查找资料,使用如下自定义的ScrollView问题解决:


public class CustomScrollView extends ScrollView {private float xDistance, yDistance, xLast, yLast;public CustomScrollView(Context context) {super(context);}public CustomScrollView(Context context, AttributeSet attrs) {super(context, attrs);}public CustomScrollView(Context context, AttributeSet attrs,int defStyle) {super(context, attrs, defStyle);}@Overridepublic 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);}}

0 0