话不多说,很简单,可以从注释中知道做法,直接上代码:
1、修改ScrollView的滑动速度:
public class MyHorizontalScrollView extends HorizontalScrollView { public MyHorizontalScrollView(Context context) { super(context); } public MyHorizontalScrollView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public MyHorizontalScrollView(Context context, AttributeSet attrs) { super(context, attrs); } @Override public void fling(int velocityX) { //重写fling方法,将速度除以三,减缓其滑动速度 super.fling(velocityX / 3); } }
2、解决ScrollView与ViewPager的冲突:
public class PagerScrollView extends ScrollView { private float xDistance, yDistance, xLast, yLast; public PagerScrollView(Context context) { super(context); } public PagerScrollView(Context context, AttributeSet attrs) { super(context, attrs); } public PagerScrollView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @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轴滑动距离,也就是用户横向滑动时,返回false,ScrollView不处理这次事件, * 让子控件中的TouchEvent去处理,所以横向滑动的事件交由ViewPager处理, * ScrollView只处理纵向滑动事件 */ if (xDistance > yDistance) { return false; } } return super.onInterceptTouchEvent(ev); } }
修改ScrollView的滑动速度和解决ScrollView与ViewPager的冲突
原文地址:http://blog.csdn.net/myatlantis/article/details/41082063