Skip to content

Commit 1f7e494

Browse files
committed
add log and abort scroll when touch
1 parent bcd21fe commit 1f7e494

File tree

1 file changed

+46
-25
lines changed

1 file changed

+46
-25
lines changed

nestedtouch/src/main/java/mobile/yy/com/nestedtouch/StickyNestedLayout.kt

+46-25
Original file line numberDiff line numberDiff line change
@@ -259,18 +259,23 @@ open class StickyNestedLayout : LinearLayout,
259259
}
260260

261261
if (mScroller.isFinished) {
262-
if (inStateOfFling) {
263-
stopNestedScroll(TYPE_NON_TOUCH)
264-
inStateOfFling = false
265-
}
266-
isNestedScrollingStartedByChild = false
267-
isNestedScrollingStartedByThisView = false
262+
abortScrollerAnimation()
268263
} else {
269264
ViewCompat.postInvalidateOnAnimation(this)
270265
}
271266
}
272267
}
273268

269+
private fun abortScrollerAnimation() {
270+
mScroller.abortAnimation()
271+
if (inStateOfFling) {
272+
stopNestedScroll(TYPE_NON_TOUCH, "abortScrollerAnimation")
273+
inStateOfFling = false
274+
}
275+
isNestedScrollingStartedByChild = false
276+
isNestedScrollingStartedByThisView = false
277+
}
278+
274279
private fun fling(vx: Float, vy: Float) {
275280
log { "startFling velocityY = $vy" }
276281
mScroller.fling(
@@ -280,7 +285,7 @@ open class StickyNestedLayout : LinearLayout,
280285
lastFlingX = 0
281286
lastFlingY = 0
282287
inStateOfFling = true
283-
startNestedScroll(ViewCompat.SCROLL_AXIS_VERTICAL, TYPE_NON_TOUCH)
288+
startNestedScroll(ViewCompat.SCROLL_AXIS_VERTICAL, TYPE_NON_TOUCH, "fling")
284289
ViewCompat.postInvalidateOnAnimation(this)
285290
}
286291

@@ -298,17 +303,24 @@ open class StickyNestedLayout : LinearLayout,
298303

299304
override fun isNestedScrollingEnabled() = childHelper.isNestedScrollingEnabled
300305

301-
override fun startNestedScroll(axes: Int) = startNestedScroll(axes, TYPE_TOUCH)
306+
override fun startNestedScroll(axes: Int) =
307+
startNestedScroll(axes, TYPE_TOUCH, "callStartNestedScroll")
308+
309+
override fun startNestedScroll(axes: Int, type: Int): Boolean =
310+
startNestedScroll(axes, type, "callStartNestedScroll(type)")
302311

303-
override fun startNestedScroll(axes: Int, type: Int): Boolean {
304-
log { "startNestedScroll $type" }
312+
private fun startNestedScroll(axes: Int, type: Int, reason: String): Boolean {
313+
log { "startNestedScroll $type by $reason" }
305314
return childHelper.startNestedScroll(axes, type)
306315
}
307316

308-
override fun stopNestedScroll() = stopNestedScroll(TYPE_TOUCH)
317+
override fun stopNestedScroll() = stopNestedScroll(TYPE_TOUCH, "CallStopNestedScroll")
309318

310-
override fun stopNestedScroll(type: Int) {
311-
log { "stopNestedScroll $type" }
319+
override fun stopNestedScroll(type: Int) =
320+
stopNestedScroll(TYPE_TOUCH, "CallStopNestedScroll(type)")
321+
322+
private fun stopNestedScroll(type: Int, reason: String) {
323+
log { "stopNestedScroll $type by $reason" }
312324
childHelper.stopNestedScroll(type)
313325
}
314326

@@ -394,7 +406,11 @@ open class StickyNestedLayout : LinearLayout,
394406
isNestedScrollingStartedByThisView = false
395407
isNestedScrollingStartedByChild = true
396408
//开始通知parent的嵌套滑动
397-
startNestedScroll(nestedScrollAxes or ViewCompat.SCROLL_AXIS_VERTICAL, type)
409+
startNestedScroll(
410+
nestedScrollAxes or ViewCompat.SCROLL_AXIS_VERTICAL,
411+
type,
412+
"onStartNestedScroll"
413+
)
398414
return true
399415
}
400416

@@ -406,7 +422,7 @@ open class StickyNestedLayout : LinearLayout,
406422
isNestedScrollingStartedByThisView = false
407423
isNestedScrollingStartedByChild = false
408424
}
409-
stopNestedScroll(type) //结束parent的嵌套滑动
425+
stopNestedScroll(type, "onStopNestedScroll") //结束parent的嵌套滑动
410426
}
411427

412428
override fun onNestedPreScroll(target: View, dx: Int, dy: Int, consumed: IntArray?, type: Int) {
@@ -512,20 +528,24 @@ open class StickyNestedLayout : LinearLayout,
512528

513529
fling(vx, vy)
514530
}
515-
stopNestedScroll(TYPE_TOUCH)
531+
stopNestedScroll(TYPE_TOUCH, "onFling")
516532
return true
517533
}
518534
return false
519535
}
520536

521537
override fun onDown(e: MotionEvent): Boolean {
522538
log { "onDown $e" }
523-
mScroller.abortAnimation()
539+
abortScrollerAnimation()
524540
lastY = e.y
525541
lastX = e.x
526-
isNestedScrollingStartedByThisView = true
527-
isNestedScrollingStartedByChild = false
528-
startNestedScroll(ViewCompat.SCROLL_AXIS_VERTICAL)
542+
if (e.x in headView.left..headView.right &&
543+
e.y in headView.top..headView.bottom
544+
) {
545+
isNestedScrollingStartedByThisView = true
546+
isNestedScrollingStartedByChild = false
547+
startNestedScroll(ViewCompat.SCROLL_AXIS_VERTICAL, TYPE_TOUCH, "onDown")
548+
}
529549
return true
530550
}
531551
}
@@ -541,7 +561,10 @@ open class StickyNestedLayout : LinearLayout,
541561
) {
542562
log { if (action == MotionEvent.ACTION_UP) "onUp" else "onCancel" }
543563
if (isNestedScrollingStartedByThisView) {
544-
stopNestedScroll(TYPE_TOUCH)
564+
stopNestedScroll(
565+
TYPE_TOUCH,
566+
if (action == MotionEvent.ACTION_UP) "onUp" else "onCancel"
567+
)
545568
return true
546569
}
547570
return false
@@ -561,14 +584,12 @@ open class StickyNestedLayout : LinearLayout,
561584
when (action) {
562585
MotionEvent.ACTION_DOWN -> {
563586
log { "onIntercept onDown" }
564-
mScroller.abortAnimation()
587+
abortScrollerAnimation()
565588
lastY = event.y
566589
lastX = event.x
567590
downRawY = event.rawY
568591
downRawX = event.rawX
569-
isNestedScrollingStartedByThisView = false
570-
isNestedScrollingStartedByChild = false
571-
startNestedScroll(ViewCompat.SCROLL_AXIS_VERTICAL)
592+
startNestedScroll(ViewCompat.SCROLL_AXIS_VERTICAL, TYPE_TOUCH, "onInterceptDown")
572593
}
573594
MotionEvent.ACTION_MOVE -> {
574595
lastY = event.y

0 commit comments

Comments
 (0)