来源:
能够兼容ViewPager的ScrollView http://justwyy.iteye.com/blog/1567390
ScrollView中嵌入ViewPager,ViewPager的滑动出现问题 http://bbs.csdn.net/topics/390213480
在网上找到两种方法,都是自定义scrollview,大家可以试试。
方法一:
public class CustomScrollView extends ScrollView { private boolean canScroll; private GestureDetector mGestureDetector; View.OnTouchListener mGestureListener; public CustomScrollView(Context context, AttributeSet attrs) { super(context, attrs); mGestureDetector = new GestureDetector(new YScrollDetector()); canScroll = true; } @Override public boolean onInterceptTouchEvent(MotionEvent ev) { if(ev.getAction() == MotionEvent.ACTION_UP) canScroll = true; return super.onInterceptTouchEvent(ev) && mGestureDetector.onTouchEvent(ev); } class YScrollDetector extends SimpleOnGestureListener { @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { if(canScroll) if (Math.abs(distanceY) >= Math.abs(distanceX)) canScroll = true; else canScroll = false; return canScroll; } } }
方法二:
public class CustomScrollView extends ScrollView { // 滑动距离及坐标 private float xDistance, yDistance, xLast, yLast; public CustomScrollView(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); } }
时间: 2024-10-31 07:13:15