Skip to content

Commit 5ab7201

Browse files
committed
add pointer down action to support multi touch in bar layout
1 parent 6ced253 commit 5ab7201

File tree

1 file changed

+29
-3
lines changed

1 file changed

+29
-3
lines changed

nestrefresh/src/main/java/com/todou/nestrefresh/base/RefreshHeaderBehavior.kt

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ import com.todou.nestrefresh.base.State.STATE_COLLAPSED
1818
import com.todou.nestrefresh.base.State.STATE_DRAGGING
1919
import com.todou.nestrefresh.base.State.STATE_HOVERING
2020
import com.todou.nestrefresh.base.State.STATE_SETTLING
21+
import android.icu.lang.UCharacter.GraphemeClusterBreak.V
22+
import android.support.v4.widget.ViewDragHelper.INVALID_POINTER
23+
2124

2225
abstract class RefreshHeaderBehavior<V : View> : BaseBehavior<V>, RefreshHeader {
2326
private var flingRunnable: Runnable? = null
@@ -52,7 +55,7 @@ abstract class RefreshHeaderBehavior<V : View> : BaseBehavior<V>, RefreshHeader
5255
this.touchSlop = ViewConfiguration.get(parent.context).scaledTouchSlop
5356
}
5457
val action = ev.action
55-
if (action == 2 && this.isBeingDragged) {
58+
if (action == MotionEvent.ACTION_MOVE && this.isBeingDragged) {
5659
return true
5760
} else {
5861
val activePointerId: Int
@@ -62,19 +65,22 @@ abstract class RefreshHeaderBehavior<V : View> : BaseBehavior<V>, RefreshHeader
6265
if (childInHeaderCanScroll(child, ev.rawX, ev.rawY)) {
6366
return super.onInterceptTouchEvent(parent, child, ev)
6467
}
65-
resetTotalSpringOffset()
68+
isTouching = false
6669
this.isBeingDragged = false
6770
activePointerId = ev.x.toInt()
6871
pointerIndex = ev.y.toInt()
6972
if (this.canDragView(child) && parent.isPointInChildBounds(child, activePointerId, pointerIndex)) {
7073
this.lastMotionY = pointerIndex
7174
this.activePointerId = ev.getPointerId(0)
7275
this.ensureVelocityTracker()
76+
onTouchStart()
77+
resetTotalSpringOffset()
7378
}
7479
}
7580
MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {
7681
this.isBeingDragged = false
7782
isTouching = false
83+
doOnCancel()
7884
this.activePointerId = -1
7985
if (this.velocityTracker != null) {
8086
this.velocityTracker?.recycle()
@@ -83,7 +89,7 @@ abstract class RefreshHeaderBehavior<V : View> : BaseBehavior<V>, RefreshHeader
8389
}
8490
MotionEvent.ACTION_MOVE -> {
8591
activePointerId = this.activePointerId
86-
if (activePointerId != -1) {
92+
if (activePointerId != INVALID_POINTER) {
8793
pointerIndex = ev.findPointerIndex(activePointerId)
8894
if (pointerIndex != -1) {
8995
val y = ev.getY(pointerIndex).toInt()
@@ -105,6 +111,16 @@ abstract class RefreshHeaderBehavior<V : View> : BaseBehavior<V>, RefreshHeader
105111
}
106112
}
107113

114+
private fun onPointerUp(e: MotionEvent) {
115+
val actionIndex = e.actionIndex
116+
if (e.getPointerId(actionIndex) == activePointerId) {
117+
// Pick a new pointer to pick up the slack.
118+
val newIndex = if (actionIndex == 0) 1 else 0
119+
activePointerId = e.getPointerId(newIndex)
120+
lastMotionY = (e.getY(newIndex) + 0.5f).toInt()
121+
}
122+
}
123+
108124
protected open fun childInHeaderCanScroll(view: V, x: Float, y: Float): Boolean {
109125
return false
110126
}
@@ -115,6 +131,7 @@ abstract class RefreshHeaderBehavior<V : View> : BaseBehavior<V>, RefreshHeader
115131
}
116132

117133
val activePointerIndex: Int
134+
var actionIndex = ev.actionIndex
118135
val y: Int
119136
when (ev.actionMasked) {
120137
MotionEvent.ACTION_DOWN -> {
@@ -128,6 +145,14 @@ abstract class RefreshHeaderBehavior<V : View> : BaseBehavior<V>, RefreshHeader
128145
this.activePointerId = ev.getPointerId(0)
129146
this.ensureVelocityTracker()
130147
onTouchStart()
148+
resetTotalSpringOffset()
149+
}
150+
MotionEvent.ACTION_POINTER_DOWN -> {
151+
this.activePointerId = ev.getPointerId(actionIndex)
152+
lastMotionY = (ev.getY(actionIndex) + 0.5f).toInt()
153+
}
154+
MotionEvent.ACTION_POINTER_UP -> {
155+
onPointerUp(ev)
131156
}
132157
MotionEvent.ACTION_UP -> {
133158
this.isTouching = false
@@ -145,6 +170,7 @@ abstract class RefreshHeaderBehavior<V : View> : BaseBehavior<V>, RefreshHeader
145170
}
146171
MotionEvent.ACTION_CANCEL -> {
147172
this.isBeingDragged = false
173+
isTouching = false
148174
this.activePointerId = -1
149175
if (this.velocityTracker != null) {
150176
this.velocityTracker?.recycle()

0 commit comments

Comments
 (0)