经常会有这种需求, 需要将一个HorizontalScrollView嵌入到一个ScrollView中, 实现既可以水平滚动, 又可以垂直滚动,但是这样发现水平滚动十分卡顿, 几乎不能使用, 原因是水平滚动事件被ScrollView消耗掉了, 这样需要重写ScrollView, 如下:
public class QScrollView extends ScrollView{
private float mDownPosX = 0;
private float mDownPosY = 0;
public QScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
final float x = ev.getX();
final float y = ev.getY();
final int action = ev.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
mDownPosX = x;
mDownPosY = y;
break;
case MotionEvent.ACTION_MOVE:
final float deltaX = Math.abs(x - mDownPosX);
final float deltaY = Math.abs(y - mDownPosY);
if (deltaX > deltaY) {
return false;
}
}
return super.onInterceptTouchEvent(ev);
}
}
便可以:)