[Android] 整理了下 ViewDragHelper 源码,整个工作流程大概是这样

android Dec 09, 2019

画了时序图,思路比较清晰了。

diagram--1--1

时序图是在这个网站画的,代码化画图,很极客,非常棒。
贴上代码以备修改:

DragView -> ViewDragHelper: create()

Note over DragView: onInterceptTouchEvent()
DragView -> ViewDragHelper: shouldInterceptTouchEvent()
Note over ViewDragHelper: ActionDown
ViewDragHelper -> Callback: tryCaptureView()
Callback --> ViewDragHelper: yes or no
ViewDragHelper -> Callback: onEdgeTouched()
Note over ViewDragHelper: ActionMove
ViewDragHelper -> Callback: clampViewPositionHorizontal()
Callback --> ViewDragHelper: newLeft
ViewDragHelper -> Callback: clampViewPositionVertical()
Callback --> ViewDragHelper: newTop
ViewDragHelper -> Callback: getViewHorizontalDragRange()
Callback --> ViewDragHelper: hDragRange
ViewDragHelper -> Callback: getViewVerticalDragRange()
Callback --> ViewDragHelper: vDragRange
ViewDragHelper -> ViewDragHelper: Check if drag satisfied
ViewDragHelper --> DragView : mDragState == STATE_DRAGGING

Note over DragView: onTouchEvent
DragView -> ViewDragHelper: processTouchEvent()

Note over ViewDragHelper: ActionDown
ViewDragHelper -> Callback: tryCaptureView()
Callback --> ViewDragHelper: yes or no
ViewDragHelper -> Callback: onEdgeTouched()
Note over ViewDragHelper: ActionMove
ViewDragHelper -> Callback: clampViewPositionHorizontal()
Callback --> ViewDragHelper: newLeft
ViewDragHelper -> Callback: clampViewPositionVertical()
Callback --> ViewDragHelper: newTop
ViewDragHelper -> ViewDragHelper: dragTo(captureView, newLeft, newTop)
ViewDragHelper -> Callback: onViewPositionChanged()

Note over ViewDragHelper: ActionUp/ActionCancel
ViewDragHelper -> Callback: onViewReleased()
/* 看板娘 */