转载自:http://bluthmatter.blog.163.com/blog/static/1842940592013653510429/
ScrollView种嵌套ViewPager
滑动看起来是不是很流畅
需要改写ScrollView
就是在触摸事件拦截器里面
判断,如果横着滑动的距离大于竖着滑动的距离,就返回false也就是不拦截当前事件,传递给下一层执行
public class ScrollViewExtend extends ScrollView{
private float xDistance;
private float yDistance;
private float xLast;
private float yLast;
public ScrollViewExtend(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
public ScrollViewExtend(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public ScrollViewExtend(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
xDistance = yDistance = 0.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);
if(xDistance > yDistance)
return false;
break;
default:
break;
}
return super.onInterceptTouchEvent(ev);
}
}