话不多说,非常easy,能够从凝视中知道做法,直接上代码:

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);
}
}

04-18 03:11