这是重现现象的视图实现:
public class MyView extends View { GestureDetector scrollGestureDetector; public MyView(Context context,AttributeSet attrs) { super(context,attrs); scrollGestureDetector = new GestureDetector(context,new GestureDetector.SimpleOnGestureListener() { @Override public boolean onScroll(final MotionEvent e1,final MotionEvent e2,final float distanceX,final float distanceY) { System.out.println("SCROLL " + distanceX + "," + distanceY); return true; } }); } @Override public boolean onTouchEvent(MotionEvent event) { scrollGestureDetector.onTouchEvent(event); return true; } }
这种行为是已知/记录/想要的吗?有没有已知的解决方法?
解决方法
scrollGestureDetector = new GestureDetector(context,new GestureDetector.SimpleOnGestureListener() { @Override public boolean onScroll(final MotionEvent e1,final float distanceY) { System.out.println("SCROLL " + distanceX + "," + distanceY); return true; } @Override public boolean onDown(MotionEvent e) { return true; } });
因为根据this document和this guide:
Notified when a tap occurs with the down MotionEvent that triggered
it. This will be triggered immediately for every down event. All other
events should be preceded by this.
和
Whether or not you use GestureDetector.OnGestureListener,it’s best
practice to implement an onDown() method that returns true. This is
because all gestures begin with an onDown() message. If you return
false from onDown(),as GestureDetector.SimpleOnGestureListener does
by default,the system assumes that you want to ignore the rest of the
gesture,and the other methods of GestureDetector.OnGestureListener
never get called. This has the potential to cause unexpected problems
in your app. The only time you should return false from onDown() is if
you truly want to ignore an entire gesture.
您需要在onDown中返回true,以便触发onScroll.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。