简体   繁体   English

如何正确理解Android的onScroll GestureDetector方法?

[英]How to properly make sense of Android's onScroll GestureDetector method?

After several StackOverflow posts and Android's onScroll documentation, I am still unable to make sense of how onScroll method works. 在发表了几篇StackOverflow和Android的onScroll文档之后,我仍然无法理解onScroll方法的工作原理。

Context and What I am trying to achieve: I have a webview with mostly texts in it. 上下文以及我要实现的目标:我有一个Web视图,其中大部分是文本。 I am overriding webview's default scrolling behavior, and instead trying to take control of the scrolling mechanism. 我重写了Webview的默认滚动行为,而是尝试控制滚动机制。 I want to be able to - 我希望能够-

  1. Scroll by a paragraph length with a fling gesture. 用甩动的手势滚动一段段落的长度。 I am able to do that by calling Android's onFling method. 我可以通过调用Android的onFling方法来做到这onFling
  2. Scroll to next sentence with a drag gesture. 用拖动手势滚动到下一个句子。 This is where I am struggling. 这就是我在努力的地方。

From the definition of drag gesture (plus scrolling gesture) in Android, the difference between drag and fling is that, for fling, the user's finger has to leave the surface with a velocity. 根据Android中拖动手势(加上滚动手势)的定义,拖动和甩动之间的区别在于,对于甩动,用户的手指必须以一定速度离开表面。 Having said that, I see that onScroll is called multiple times and onFling only once. 话虽如此,我看到onScroll被多次调用,而onFling仅被调用一次。 I guess that is okay. 我想那没关系。 What I am struggling to understand is what does the parameters do in onScroll method? 我正在努力了解的是onScroll方法中的参数做什么?

I don't quite understand the purpose of event1 , event2 , distanceX , and distanceY - if all of them change each time onScroll is called. 我不太了解event1event2distanceXdistanceY的用途-如果每次调用onScroll它们都改变。 How do you make sense of these parameters? 您如何理解这些参数? What is the proper way of interpreting them? 解释它们的正确方法是什么? Android documentation did not help. Android文档没有帮助。

UPDATE 更新

I logged my touch movements, and here is how it looks. 我记录了我的触摸动作,这就是它的外观。

Movement: Dragging vertically up without lifting the finger 移动:垂直向上拖动而不用举起手指

event 1 X=600.64453, Y=891.5178
event 2 X=594.5489, Y=891.5178
event 1 X=600.64453, Y=886.3
event 2 X=593.7481, Y=886.3
event 1 X=600.64453, Y=879.9082
event 2 X=592.97614, Y=879.9082

Question: Since I moved vertically up, I expect most of my changes in Y axis, and very minimal in X axis. 问题:由于我是垂直向上移动的,所以我希望我的大部分变化在Y轴上,而在X轴上的变化很小。 What I see here is that, for event 1, X is always the same, however, Y keeps changing. 我在这里看到的是,对于事件1,X始终相同,但是Y一直在变化。 If event 1 is the first down motion that started the scrolling, shouldn't Y still be the same? 如果事件1是开始滚动的第一个下移动作,那么Y是否还应该相同? But even then, how do you explain why for any pair of {event 1, event 2} here, Xs are different whereas Ys are same? 但是即使如此,您如何解释为什么这里的{event 1,event 2}中的任何一对X都不相同而Y相同? Shouldn't it be the opposite? 不应该相反吗?

Movement: Dragging horizontally from left to right. 移动:从左向右水平拖动。

event 1 X=340.66406, Y=861.7873
event 2 X=374.05276, Y=861.7873
event 1 X=340.66406, Y=862.9392
event 2 X=382.84317, Y=862.9392
event 1 X=340.66406, Y=864.10034
event 2 X=390.39926, Y=864.10034

Question: Intuition says, movement in horizontal direction should trigger most changes in X axis and minor changes in Y axis. 问题:凭直觉说,在水平方向上的移动应触发X轴上的大部分更改,而Y轴上的次要更改。 Here, all event 1 Xs are same, but Y is different. 在此,所有事件1 X相同,但Y不同。 Again, I expected both X and Y to be same! 同样,我期望X和Y相同! For any pair of {event 1, event 2}, Xs are different whereas Ys are same. 对于{事件1,事件2}的任何一对,X都是不同的,而Y是相同的。 This sounds normal, but then again, if I look back to the one above, I am getting confused. 这听起来很正常,但是如果再次回顾以上内容,我会感到困惑。 How do you distinguish them? 您如何区分它们?

Thanks and my apology for a long verbose question! 谢谢您,我很抱歉冗长的问题!

From the docs: 从文档:

/**
  * Notified when a scroll occurs with the initial on down {@link MotionEvent} and the
  * current move {@link MotionEvent}. The distance in x and y is also supplied for
  * convenience.
  *
  * @param e1 The first down motion event that started the scrolling.
  * @param e2 The move motion event that triggered the current onScroll.
  * @param distanceX The distance along the X axis that has been scrolled since the last
  *              call to onScroll. This is NOT the distance between {@code e1}
  *              and {@code e2}.
  * @param distanceY The distance along the Y axis that has been scrolled since the last
  *              call to onScroll. This is NOT the distance between {@code e1}
  *              and {@code e2}.
  * @return true if the event is consumed, else false
  */
  boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY);

e1 : Since this is the motion event that triggered the scroll, you can expect this to stay the same throughout your scroll. e1 :由于这是触发滚动的运动事件,因此可以期望在整个滚动中保持不变。

e2 : A user could move their finger around the screen in multiple directions without lifting it and onScroll would be called separately for each change it detects. e2 :用户可以沿多个方向在屏幕上移动手指而不用抬起它,并且它检测到的每个更改都会分别调用onScroll This is pretty sensitive, so that's why you're seeing onScroll called multiple times. 这非常敏感,所以这就是为什么您多次看到onScroll的原因。

distanceX and distanceY aren't your total distances traveled, but only the distances since the last time onScroll was called. distanceXdistanceY并不是您的总行驶距离,而仅是自上次调用onScroll以来的距离。 You can calculate your net distances by comparing the getX() and getY() methods on each MotionEvent. 您可以通过比较每个MotionEvent的getX()getY()方法来计算净距离。

If all you need to do is check that a scroll was initiated, this is probably more info than you need. 如果您需要做的只是检查滚动是否已启动,这可能是您需要的更多信息。 You could try and trigger your event only when you receive a unique event1 你可以尝试,只有当你收到一个唯一的触发事件event1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用onScroll和GestureDetector - Android跟随我的手指 - How to make a View follow my finger using onScroll and GestureDetector - Android Android GestureDetector onScroll意外行为 - Android GestureDetector onScroll unexpected behavior 手势检测器 onScroll() 方法上的随机滚动行为 - Random scroll behavour on gestureDetector onScroll() method Android的GestureDetector中“onFling”和“onScroll”事件的目的是什么? - What is the purpose of “onFling” and “onScroll” event in GestureDetector of Android? Android GestureDetector无法使用FrameLayout检测onScroll事件 - Android GestureDetector cannot detect onScroll event with FrameLayout android.view.GestureDetector.OnGestureListener onFling()vs onScroll() - android.view.GestureDetector.OnGestureListener onFling() vs onScroll() 使用 GestureDetector onScroll 滚动延迟 - Scrolling lag with GestureDetector onScroll GestureDetector的onScroll()和onFling()之间的区别 - Difference between onScroll() and onFling() of GestureDetector 如何在Android中的GestureDetector中使用Onitemclicklistner? - How to use Onitemclicklistner with GestureDetector in android? 如何获取图像按钮的尺寸以在android中有意义? - How can I get an image button's dimensions to make sense in android?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM