时间:2022-02-08 11:52:59 | 栏目:Android代码 | 点击:次
实例如下:
public class CustomScrollView extends ScrollView { private GestureDetector mGestureDetector; View.OnTouchListener mGestureListener; @SuppressWarnings("deprecation") public CustomScrollView(Context context,AttributeSet attrs) { super(context,attrs); mGestureDetector= new GestureDetector(new YScrollDetector()); setFadingEdgeLength(0); } //通过手势判断,来判断是否拦截触摸事件。 @Override public boolean onInterceptTouchEvent(MotionEvent ev) { return super.onInterceptTouchEvent(ev) && mGestureDetector.onTouchEvent(ev); } // Return false if we're scrolling in the x direction class YScrollDetector extends SimpleOnGestureListener { @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { if(Math.abs(distanceY) > Math.abs(distanceX)) { return true; } return false; } } }