1 package com.example.huanshop.widget; 2 3 import android.content.Context; 4 import android.util.AttributeSet; 5 import android.view.MotionEvent; 6 import android.widget.ScrollView; 7 8 public class MyScrollView extends ScrollView { 9 10 private float xDistance; 11 private float yDistance; 12 private float xLast; 13 private float yLast; 14 15 public MyScrollView(Context context) { 16 super(context); 17 // TODO Auto-generated constructor stub 18 } 19 20 public MyScrollView(Context context, AttributeSet attrs, int defStyle) { 21 super(context, attrs, defStyle); 22 // TODO Auto-generated constructor stub 23 } 24 25 public MyScrollView(Context context, AttributeSet attrs) { 26 super(context, attrs); 27 // TODO Auto-generated constructor stub 28 } 29 30 @Override 31 public boolean onInterceptTouchEvent(MotionEvent ev) { 32 switch (ev.getAction()) { 33 case MotionEvent.ACTION_DOWN: 34 xDistance = yDistance = 0.0f; 35 xLast = ev.getX(); 36 yLast = ev.getY(); 37 break; 38 case MotionEvent.ACTION_MOVE: 39 final float curX = ev.getX(); 40 final float curY = ev.getY(); 41 xDistance += Math.abs(curX - xLast); 42 yDistance += Math.abs(curY - yLast); 43 if (xDistance > yDistance) 44 return false; 45 break; 46 default: 47 break; 48 } 49 return super.onInterceptTouchEvent(ev); 50 } 51 52 }
这 段 代码 什么用
这个主要用于ScrollView嵌套ScrollViewer的场景。
具体到你的代码,意思应该是在监听滑动操作时,如果水平方向的移动距离大于垂直方向的移动距离,则上层的ScrollView可以监听到滑动事件,否则监听不到