1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
| class CustomNestedChild @JvmOverloads constructor( context: Context?, attrs: AttributeSet? = null, defStyleAttr: Int = 0 ) : LinearLayout(context, attrs, defStyleAttr), NestedScrollingChild2 {
val TAG = "CustomNestedChild"
private val mScrollingChildHelper = NestedScrollingChildHelper(this)
private val viewConfiguration: ViewConfiguration = ViewConfiguration.get(context) private var mVelocityTracker: VelocityTracker? = null
private val mScroller: Scroller = Scroller(context)
private var mLastX: Float = 0f private var mLastY: Float = 0f private var mLastFlingX: Float = 0f private var mLastFlingY: Float = 0f
private val offset = IntArray(2) private val consumed = IntArray(2) private var fling = false
private var childrenHeight = 0
init { orientation = VERTICAL isNestedScrollingEnabled = true mScrollingChildHelper.isNestedScrollingEnabled = true }
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) { var height = MeasureSpec.getSize(heightMeasureSpec) var width = MeasureSpec.getSize(widthMeasureSpec) childrenHeight = 0 for (i in 0 until childCount) { val child = getChildAt(i) measureChild(child, widthMeasureSpec, MeasureSpec.UNSPECIFIED) childrenHeight += child.measuredHeight } setMeasuredDimension(width, height) }
override fun startNestedScroll(axes: Int, type: Int): Boolean { return mScrollingChildHelper.startNestedScroll(axes, type) }
override fun stopNestedScroll(type: Int) { mScrollingChildHelper.stopNestedScroll(type) }
override fun hasNestedScrollingParent(type: Int): Boolean { return mScrollingChildHelper.hasNestedScrollingParent() }
override fun dispatchNestedPreScroll( dx: Int, dy: Int, consumed: IntArray?, offsetInWindow: IntArray?, type: Int ): Boolean { return mScrollingChildHelper.dispatchNestedPreScroll(dx, dy, consumed, offsetInWindow, type) }
override fun dispatchNestedScroll( dxConsumed: Int, dyConsumed: Int, dxUnconsumed: Int, dyUnconsumed: Int, offsetInWindow: IntArray?, type: Int ): Boolean { return mScrollingChildHelper.dispatchNestedScroll( dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed, offsetInWindow, type ) }
override fun onTouchEvent(event: MotionEvent): Boolean { cancelFling()
if (mVelocityTracker == null) { mVelocityTracker = VelocityTracker.obtain() } val velocityTracker = mVelocityTracker!! velocityTracker.addMovement(event)
when (event.action) { MotionEvent.ACTION_DOWN -> { mLastX = event.x mLastY = event.y startNestedScroll(ViewCompat.SCROLL_AXIS_VERTICAL, TYPE_TOUCH) } MotionEvent.ACTION_MOVE -> { val curX = event.x val curY = event.y var dy = (mLastY - curY).toInt() var dx = (mLastX - curX).toInt() if (dispatchNestedPreScroll(dx, dy, consumed, offset, TYPE_TOUCH)) { dy -= consumed[1] dx -= consumed[0] } val consumedY = childConsumeY(dy) dispatchNestedScroll(0, consumedY, dx, dy - consumedY, null, TYPE_TOUCH) mLastX = curX mLastY = curY } MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> { stopNestedScroll(TYPE_TOUCH)
velocityTracker.computeCurrentVelocity( 1000, viewConfiguration.scaledMaximumFlingVelocity.toFloat() ) val yvel = velocityTracker.yVelocity fling(yvel.toInt()) velocityTracker.clear() } } return true }
private fun fling(velocityY: Int) { cancelFling()
var dy: Int = velocityY if (abs(velocityY) < viewConfiguration.scaledMinimumFlingVelocity) { dy = 0 } if (dy == 0) { return }
startNestedScroll(ViewCompat.SCROLL_AXIS_VERTICAL, TYPE_NON_TOUCH)
val maxFlingVelocity: Int = viewConfiguration.scaledMaximumFlingVelocity dy = max(-maxFlingVelocity, min(dy, maxFlingVelocity))
Log.i(TAG, "fling: $dy ") fling = true mScroller.fling( 0, 0, 0, dy, Integer.MIN_VALUE, Integer.MAX_VALUE, Integer.MIN_VALUE, Integer.MAX_VALUE ) postInvalidate() }
override fun computeScroll() { if (mScroller.computeScrollOffset() && fling) { val y = mScroller.currY var dy = (mLastFlingY - y).toInt() mLastFlingY = y.toFloat() if (dispatchNestedPreScroll(0, dy, consumed, null, TYPE_NON_TOUCH)) { dy -= consumed[1] } Log.i(TAG, "computeScroll: ${consumed[1]} $dy") val consumedY = childFling(dy) dispatchNestedScroll(0, consumedY, 0, dy - consumedY, null, TYPE_NON_TOUCH) postInvalidate() } else { stopNestedScroll(TYPE_NON_TOUCH) cancelFling() } }
private fun childConsumeY(dy: Int): Int { var consumed = dy if (consumed < 0) { consumed = max(-scrollY, consumed) } else { val max = max(childrenHeight - height, 0) if (dy + scrollY > max) { consumed = max - scrollY } } Log.i(TAG, "childConsumeY: $dy $consumed $scrollY") scrollBy(0, consumed) return consumed }
private fun childFling(dy: Int): Int { return childConsumeY(dy) }
private fun cancelFling() { fling = false mLastFlingY = 0f mLastFlingY = 0f }
override fun scrollTo(x: Int, y: Int) { var resY = y if (resY < 0) { resY = 0 } val max = max(childrenHeight - height, 0) if (y > max) { resY = max } Log.i(TAG, "scrollTo: $max $y $resY") super.scrollTo(x, resY) }
override fun canScrollVertically(direction: Int): Boolean { if (direction < 0 && scrollY <= 0) { return false } else if (direction > 0 && scrollY >= measuredHeight - height) { return false } else { return true } } }
|