-
Notifications
You must be signed in to change notification settings - Fork 0
/
DragLinearLayout.kt
629 lines (575 loc) · 24.2 KB
/
DragLinearLayout.kt
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
package ru.kraynov.library.draglinearlayout
import android.animation.*
import android.animation.ValueAnimator.AnimatorUpdateListener
import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Rect
import android.graphics.drawable.BitmapDrawable
import android.util.AttributeSet
import android.util.Log
import android.util.SparseArray
import android.view.MotionEvent
import android.view.View
import android.view.ViewConfiguration
import android.view.ViewTreeObserver
import android.widget.LinearLayout
import androidx.core.view.MotionEventCompat
import kotlin.math.abs
import kotlin.math.roundToInt
class DragLinearLayout @JvmOverloads constructor(context: Context?, attrs: AttributeSet? = null) :
LinearLayout(context, attrs) {
companion object {
private val TAG = DragLinearLayout::class.java.simpleName
private const val NOMINAL_SWITCH_DURATION: Long = 150
private const val MIN_SWITCH_DURATION = NOMINAL_SWITCH_DURATION
private const val MAX_SWITCH_DURATION = NOMINAL_SWITCH_DURATION * 2
private const val NOMINAL_DISTANCE = 20f
private const val INVALID_POINTER_ID = -1
private fun getBitmapFromView(view: View): Bitmap {
val bitmap = Bitmap.createBitmap(view.width, view.height, Bitmap.Config.ARGB_8888)
val canvas = Canvas(bitmap)
view.draw(canvas)
return bitmap
}
}
private val mNominalDistanceScaled: Float
private val mDragItem: DragItem
private val mSlop: Int
private var mDownY = -1
private var mDownX = -1
private var mActivePointerId = INVALID_POINTER_ID
private var mLayoutTransition: LayoutTransition? = null
private val mDraggableChildren: SparseArray<DraggableChild> = SparseArray()
private var mIsLongClickDraggable = false
private var mClickToDragListener: ILongClickToDragListener? = null
private var mIsEnterLongClick = false
private val mLongClickDragListener = LongClickDragListener()
private var swapListener: OnViewSwapListener? = null
init {
mDragItem = DragItem()
val vc = ViewConfiguration.get(context)
mSlop = vc.scaledTouchSlop
val resources = resources
mNominalDistanceScaled = (NOMINAL_DISTANCE * resources.displayMetrics.density + 0.5f).roundToInt().toFloat()
}
fun addDragView(child: View?, dragHandle: View?) {
addView(child)
setViewDraggable(child, dragHandle)
}
fun setViewDraggable(child: View?, dragHandle: View?) {
require(!(null == child || null == dragHandle)) { "Draggable children and their drag handles must not be null." }
if (this === child!!.parent) {
dragHandle!!.setOnTouchListener(DragHandleOnTouchListener(child!!))
dragHandle!!.setOnLongClickListener(mLongClickDragListener)
mDraggableChildren.put(indexOfChild(child), DraggableChild())
} else {
Log.e(TAG, "$child is not a child, cannot make draggable.")
}
}
override fun removeAllViews() {
val count = childCount
for (i in 0 until count) {
getChildAt(i).setOnLongClickListener(null)
getChildAt(i).setOnTouchListener(null)
}
super.removeAllViews()
mDraggableChildren.clear()
}
private fun getTranslateAnimationDuration(distance: Float): Long {
return Math.min(
MAX_SWITCH_DURATION, Math.max(
MIN_SWITCH_DURATION,
(NOMINAL_SWITCH_DURATION * Math.abs(distance) / mNominalDistanceScaled).toLong()
)
)
}
fun startDetectingDrag(child: View) {
if (mDragItem.mDetecting) return
val position = indexOfChild(child)
if (position >= 0) {
mDraggableChildren[position].endExistingAnimation()
mDragItem.startDetectingOnPossibleDrag(child, position)
}
}
private fun startDrag() {
swapListener?.onStartDrag()
mLayoutTransition = layoutTransition
if (mLayoutTransition != null) {
layoutTransition = null
}
mDragItem.onDragStart()
requestDisallowInterceptTouchEvent(true)
}
private fun onDragStop() {
if (orientation == VERTICAL) {
mDragItem.mSettleAnimation = ValueAnimator.ofFloat(
mDragItem.mTotalDragOffset.toFloat(),
mDragItem.mTotalDragOffset - mDragItem.mTargetTopOffset.toFloat()
)
.setDuration(getTranslateAnimationDuration(mDragItem.mTargetTopOffset.toFloat()))
} else {
mDragItem.mSettleAnimation = ValueAnimator.ofFloat(
mDragItem.mTotalDragOffset.toFloat(),
mDragItem.mTotalDragOffset - mDragItem.mTargetLeftOffset.toFloat()
)
.setDuration(getTranslateAnimationDuration(mDragItem.mTargetLeftOffset.toFloat()))
}
mDragItem.mSettleAnimation?.addUpdateListener(AnimatorUpdateListener { animation ->
if (!mDragItem.mDetecting) return@AnimatorUpdateListener
mDragItem.setTotalOffset((animation.animatedValue as Float).toInt())
invalidate()
})
mDragItem.mSettleAnimation?.addListener(object : AnimatorListenerAdapter() {
override fun onAnimationStart(animation: Animator) {
mDragItem.onDragStop()
}
override fun onAnimationEnd(animation: Animator) {
if (!mDragItem.mDetecting) {
return
}
swapListener?.onEndDrag()
mDragItem.mSettleAnimation = null
mDragItem.stopDetecting()
if (mLayoutTransition != null && layoutTransition == null) {
layoutTransition = mLayoutTransition
}
}
})
mDragItem.mSettleAnimation?.start()
}
private fun onDrag(offset: Int) {
if (orientation == VERTICAL) {
mDragItem.setTotalOffset(offset)
invalidate()
val currentTop = mDragItem.mStartTop + mDragItem.mTotalDragOffset
val belowPosition = nextDraggablePosition(mDragItem.mPosition)
val abovePosition = previousDraggablePosition(mDragItem.mPosition)
val belowView = getChildAt(belowPosition)
val aboveView = getChildAt(abovePosition)
val isBelow = belowView != null &&
currentTop + mDragItem.mHeight > belowView.top + belowView.height / 2
val isAbove = aboveView != null &&
currentTop < aboveView.top + aboveView.height / 2
if (isBelow || isAbove) {
val switchView = if (isBelow) belowView else aboveView
val originalPosition = mDragItem.mPosition
val switchPosition = if (isBelow) belowPosition else abovePosition
mDraggableChildren[switchPosition].cancelExistingAnimation()
val switchViewStartY = switchView!!.y
swapListener?.onSwap(mDragItem.mView, mDragItem.mPosition, switchView, switchPosition)
if (isBelow) {
removeViewAt(originalPosition)
removeViewAt(switchPosition - 1)
addView(belowView, originalPosition)
addView(mDragItem.mView, switchPosition)
} else {
removeViewAt(switchPosition)
removeViewAt(originalPosition - 1)
addView(mDragItem.mView, switchPosition)
addView(aboveView, originalPosition)
}
mDragItem.mPosition = switchPosition
val switchViewObserver = switchView.viewTreeObserver
switchViewObserver.addOnPreDrawListener(object :
ViewTreeObserver.OnPreDrawListener {
override fun onPreDraw(): Boolean {
switchViewObserver.removeOnPreDrawListener(this)
val switchAnimator = ObjectAnimator.ofFloat(
switchView, "y",
switchViewStartY, switchView.top.toFloat()
)
.setDuration(getTranslateAnimationDuration(switchView.top - switchViewStartY))
switchAnimator.addListener(object : AnimatorListenerAdapter() {
override fun onAnimationStart(animation: Animator) {
mDraggableChildren[originalPosition].mValueAnimator = switchAnimator
}
override fun onAnimationEnd(animation: Animator) {
mDraggableChildren[originalPosition].mValueAnimator = null
}
})
switchAnimator.start()
return true
}
})
val observer = mDragItem.mView!!.viewTreeObserver
observer.addOnPreDrawListener(object : ViewTreeObserver.OnPreDrawListener {
override fun onPreDraw(): Boolean {
observer.removeOnPreDrawListener(this)
mDragItem.updateTargetLocation()
if (mDragItem.settling()) {
Log.d(TAG, "Updating settle animation")
mDragItem.mSettleAnimation!!.removeAllListeners()
mDragItem.mSettleAnimation!!.cancel()
onDragStop()
}
return true
}
})
}
} else {
mDragItem.setTotalOffset(offset)
invalidate()
val currentLeft = mDragItem.mStartLeft + mDragItem.mTotalDragOffset
val nextPosition = nextDraggablePosition(mDragItem.mPosition)
val prePosition = previousDraggablePosition(mDragItem.mPosition)
val nextView = getChildAt(nextPosition)
val preView = getChildAt(prePosition)
val isToNext = nextView != null &&
currentLeft + mDragItem.mWidth > nextView.left + nextView.width / 2
val isToPre = preView != null &&
currentLeft < preView.left + preView.width / 2
if (isToNext || isToPre) {
val switchView = if (isToNext) nextView else preView
val originalPosition = mDragItem.mPosition
val switchPosition = if (isToNext) nextPosition else prePosition
mDraggableChildren[switchPosition].cancelExistingAnimation()
val switchViewStartX = switchView!!.x
swapListener?.onSwap(mDragItem.mView, mDragItem.mPosition, switchView, switchPosition)
if (isToNext) {
removeViewAt(originalPosition)
removeViewAt(switchPosition - 1)
addView(nextView, originalPosition)
addView(mDragItem.mView, switchPosition)
} else {
removeViewAt(switchPosition)
removeViewAt(originalPosition - 1)
addView(mDragItem.mView, switchPosition)
addView(preView, originalPosition)
}
mDragItem.mPosition = switchPosition
val switchViewObserver = switchView.viewTreeObserver
switchViewObserver.addOnPreDrawListener(object :
ViewTreeObserver.OnPreDrawListener {
override fun onPreDraw(): Boolean {
switchViewObserver.removeOnPreDrawListener(this)
val switchAnimator = ObjectAnimator.ofFloat(
switchView, "x",
switchViewStartX, switchView.left.toFloat()
)
.setDuration(getTranslateAnimationDuration(switchView.left - switchViewStartX))
switchAnimator.addListener(object : AnimatorListenerAdapter() {
override fun onAnimationStart(animation: Animator) {
mDraggableChildren[originalPosition].mValueAnimator = switchAnimator
}
override fun onAnimationEnd(animation: Animator) {
mDraggableChildren[originalPosition].mValueAnimator = null
}
})
switchAnimator.start()
return true
}
})
val observer = mDragItem.mView!!.viewTreeObserver
observer.addOnPreDrawListener(object : ViewTreeObserver.OnPreDrawListener {
override fun onPreDraw(): Boolean {
observer.removeOnPreDrawListener(this)
mDragItem.updateTargetLocation()
if (mDragItem.settling()) {
mDragItem.mSettleAnimation!!.removeAllListeners()
mDragItem.mSettleAnimation!!.cancel()
onDragStop()
}
return true
}
})
}
}
}
private fun previousDraggablePosition(position: Int): Int {
val startIndex = mDraggableChildren.indexOfKey(position)
return if (startIndex < 1 || startIndex > mDraggableChildren.size()) -1 else mDraggableChildren.keyAt(
startIndex - 1
)
}
private fun nextDraggablePosition(position: Int): Int {
val startIndex = mDraggableChildren.indexOfKey(position)
return if (startIndex < -1 || startIndex > mDraggableChildren.size() - 2) -1 else mDraggableChildren.keyAt(
startIndex + 1
)
}
override fun dispatchDraw(canvas: Canvas) {
super.dispatchDraw(canvas)
if (mDragItem.mDetecting && (mDragItem.mDragging || mDragItem.settling())) {
canvas.save()
if (orientation == VERTICAL) {
canvas.translate(0f, mDragItem.mTotalDragOffset.toFloat())
} else {
canvas.translate(mDragItem.mTotalDragOffset.toFloat(), 0f)
}
mDragItem.mBitmapDrawable!!.draw(canvas)
canvas.restore()
}
}
override fun onInterceptTouchEvent(event: MotionEvent): Boolean {
if (!mIsLongClickDraggable) {
return super.onInterceptTouchEvent(event)
}
when (MotionEventCompat.getActionMasked(event)) {
MotionEvent.ACTION_DOWN -> {
parent.requestDisallowInterceptTouchEvent(true)
if (mDragItem.mDetecting) return false
mDownY = MotionEventCompat.getY(event, 0).toInt()
mDownX = MotionEventCompat.getX(event, 0).toInt()
mActivePointerId = MotionEventCompat.getPointerId(event, 0)
}
MotionEvent.ACTION_MOVE -> {
if (!mIsLongClickDraggable) {
return super.onInterceptTouchEvent(event)
}
if (!mDragItem.mDetecting) return false
if (INVALID_POINTER_ID == mActivePointerId)
return false
val pointerIndex = event.findPointerIndex(mActivePointerId)
val y = MotionEventCompat.getY(event, pointerIndex)
val x = MotionEventCompat.getX(event, pointerIndex)
val dy = y - mDownY
val dx = x - mDownX
if (orientation == VERTICAL) {
if (abs(dy) > mSlop) {
startDrag()
return true
}
} else {
if (abs(dx) > mSlop) {
startDrag()
return true
}
}
return false
}
MotionEvent.ACTION_POINTER_UP -> {
run {
val pointerIndex = MotionEventCompat.getActionIndex(event)
val pointerId = MotionEventCompat.getPointerId(event, pointerIndex)
if (pointerId != mActivePointerId)
return false
}
run {
parent.requestDisallowInterceptTouchEvent(false)
onTouchEnd()
if (mDragItem.mDetecting) mDragItem.stopDetecting()
}
}
MotionEvent.ACTION_CANCEL, MotionEvent.ACTION_UP -> {
parent.requestDisallowInterceptTouchEvent(false)
onTouchEnd()
if (mDragItem.mDetecting) mDragItem.stopDetecting()
}
}
return false
}
@SuppressLint("ClickableViewAccessibility")
override fun onTouchEvent(event: MotionEvent): Boolean {
if (!mIsLongClickDraggable) {
return super.onTouchEvent(event)
}
when (MotionEventCompat.getActionMasked(event)) {
MotionEvent.ACTION_DOWN -> {
if (!mDragItem.mDetecting || mDragItem.settling()) return false
startDrag()
return true
}
MotionEvent.ACTION_MOVE -> {
if (!mIsEnterLongClick) {
return super.onTouchEvent(event)
}
if (!mDragItem.mDragging)
return false
if (INVALID_POINTER_ID == mActivePointerId)
return false
val pointerIndex = event.findPointerIndex(mActivePointerId)
val lastEventY = MotionEventCompat.getY(event, pointerIndex)
.toInt()
val lastEventX = MotionEventCompat.getX(event, pointerIndex)
.toInt()
if (orientation == VERTICAL) {
val deltaY = lastEventY - mDownY
onDrag(deltaY)
} else {
val deltaX = lastEventX - mDownX
onDrag(deltaX)
}
return true
}
MotionEvent.ACTION_POINTER_UP -> {
run {
val pointerIndex = MotionEventCompat.getActionIndex(event)
val pointerId = MotionEventCompat.getPointerId(event, pointerIndex)
if (pointerId != mActivePointerId)
return false
}
run {
onTouchEnd()
if (mDragItem.mDragging) {
onDragStop()
} else if (mDragItem.mDetecting) {
mDragItem.stopDetecting()
}
return true
}
}
MotionEvent.ACTION_CANCEL, MotionEvent.ACTION_UP -> {
onTouchEnd()
if (mDragItem.mDragging) {
onDragStop()
} else if (mDragItem.mDetecting) {
mDragItem.stopDetecting()
}
return true
}
}
return false
}
private fun onTouchEnd() {
mDownY = -1
mDownX = -1
mIsEnterLongClick = false
mActivePointerId = INVALID_POINTER_ID
}
private fun getDragDrawable(view: View): BitmapDrawable {
val top = view.top
val left = view.left
val bitmap = getBitmapFromView(view)
val drawable = BitmapDrawable(resources, bitmap)
drawable.bounds = Rect(left, top, left + view.width, top + view.height)
return drawable
}
fun setClickToDragListener(clickToDragListener: ILongClickToDragListener?) {
mClickToDragListener = clickToDragListener
}
fun setLongClickDrag(longClickDrag: Boolean) {
if (mIsLongClickDraggable != longClickDrag) {
mIsLongClickDraggable = longClickDrag
}
}
fun setViewSwapListener(swapListener: OnViewSwapListener){
this.swapListener = swapListener
}
private inner class DragItem internal constructor() {
var mView: View? = null
private var mStartVisibility = 0
var mBitmapDrawable: BitmapDrawable? = null
var mPosition = 0
var mStartTop = 0
var mHeight = 0
var mTotalDragOffset = 0
var mTargetTopOffset = 0
var mStartLeft = 0
var mWidth = 0
var mTargetLeftOffset = 0
var mSettleAnimation: ValueAnimator? = null
var mDetecting = false
var mDragging = false
fun startDetectingOnPossibleDrag(view: View, position: Int) {
mView = view
mStartVisibility = view.visibility
mBitmapDrawable = getDragDrawable(view)
mPosition = position
mStartTop = view.top
mHeight = view.height
mStartLeft = view.left
mWidth = view.width
mTotalDragOffset = 0
mTargetTopOffset = 0
mTargetLeftOffset = 0
mSettleAnimation = null
mDetecting = true
}
fun onDragStart() {
mView!!.visibility = INVISIBLE
mDragging = true
}
fun setTotalOffset(offset: Int) {
mTotalDragOffset = offset
updateTargetLocation()
}
fun updateTargetLocation() {
if (orientation == VERTICAL) {
updateTargetTop()
} else {
updateTargetLeft()
}
}
private fun updateTargetLeft() {
mTargetLeftOffset = mStartLeft - mView!!.left + mTotalDragOffset
}
private fun updateTargetTop() {
mTargetTopOffset = mStartTop - mView!!.top + mTotalDragOffset
}
fun onDragStop() {
mDragging = false
}
fun settling(): Boolean {
return null != mSettleAnimation
}
fun stopDetecting() {
mDetecting = false
if (null != mView) mView!!.visibility = mStartVisibility
mView = null
mStartVisibility = -1
mBitmapDrawable = null
mPosition = -1
mStartTop = -1
mHeight = -1
mStartLeft = -1
mWidth = -1
mTotalDragOffset = 0
mTargetTopOffset = 0
mTargetLeftOffset = 0
if (null != mSettleAnimation) mSettleAnimation!!.end()
mSettleAnimation = null
}
init {
stopDetecting()
}
}
private inner class DraggableChild {
var mValueAnimator: ValueAnimator? = null
fun endExistingAnimation() {
mValueAnimator?.end()
}
fun cancelExistingAnimation() {
mValueAnimator?.cancel()
}
}
private inner class DragHandleOnTouchListener internal constructor(private val view: View) :
OnTouchListener {
@SuppressLint("ClickableViewAccessibility")
override fun onTouch(v: View, event: MotionEvent): Boolean {
if (MotionEvent.ACTION_DOWN == MotionEventCompat.getActionMasked(event)) {
startDetectingDrag(view)
}
return false
}
}
inner class LongClickDragListener : OnLongClickListener {
override fun onLongClick(v: View): Boolean {
if (!mIsLongClickDraggable) {
return false
}
mIsEnterLongClick = true
if (mClickToDragListener != null) {
mClickToDragListener!!.onLongClickToDrag(v)
}
startDetectingDrag(v)
return true
}
}
interface ILongClickToDragListener {
fun onLongClickToDrag(dragableView: View?)
}
interface OnViewSwapListener {
fun onStartDrag(){}
fun onEndDrag(){}
/**
* Invoked right before the two items are swapped due to a drag event.
* After the swap, the firstView will be in the secondPosition, and vice versa.
*
*
* No guarantee is made as to which of the two has a lesser/greater position.
*/
fun onSwap(firstView: View?, firstPosition: Int, secondView: View?, secondPosition: Int){}
}
}