/**
* 能够兼容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);
}
}
这种解决方法虽然解决了两者横向滑动的冲突,但是的缺点在于,上下滑动无法让控件响应到。
所以在这里需要对ACTIOM_MOVE进行判断,可以对XY轴的移动坐标进行比较,
代码如下
/**
* 能够兼容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;
//如果X轴移动量大于Y轴的1.5倍及以上 则返回true scrollview滑动
if(xDistance> yDistance*1.5){
return true;
}else{
return false;
}
}
return super.onInterceptTouchEvent(ev);
}
}
* 能够兼容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);
}
}
这种解决方法虽然解决了两者横向滑动的冲突,但是的缺点在于,上下滑动无法让控件响应到。
所以在这里需要对ACTIOM_MOVE进行判断,可以对XY轴的移动坐标进行比较,
代码如下
/**
* 能够兼容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;
//如果X轴移动量大于Y轴的1.5倍及以上 则返回true scrollview滑动
if(xDistance> yDistance*1.5){
return true;
}else{
return false;
}
}
return super.onInterceptTouchEvent(ev);
}
}