Advertisement
Guest User

Untitled

a guest
Dec 28th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.08 KB | None | 0 0
  1. package com.musicapp.app.utils
  2.  
  3. import android.content.Context
  4. import android.graphics.*
  5. import android.graphics.drawable.ColorDrawable
  6. import androidx.core.content.ContextCompat
  7. import androidx.recyclerview.widget.ItemTouchHelper
  8. import androidx.recyclerview.widget.RecyclerView
  9.  
  10. /**
  11. * Created by pertadima on 02,April,2019
  12. */
  13.  
  14. abstract class SwipeControl(
  15. private val context: Context,
  16. private val leftIcon: Int,
  17. private val rightIcon: Int,
  18. private val leftBackgroundColor: String = "#FFFFFF",
  19. private val rightBackgroundColor: String = "#FFFFFF"
  20. ) :
  21. ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT) {
  22.  
  23. companion object {
  24. const val SWIPE_RIGHT = 8
  25. const val SWIPE_LEFT = 4
  26. const val DEFAULT_NULL_VALUE = 0
  27. const val DIVIDE_BY_TWO = 2
  28. }
  29.  
  30. private var icon = ContextCompat.getDrawable(context, leftIcon)
  31. private val intrinsicWidth = icon?.intrinsicWidth ?: DEFAULT_NULL_VALUE
  32. private val intrinsicHeight = icon?.intrinsicHeight ?: DEFAULT_NULL_VALUE
  33. private val background = ColorDrawable()
  34. private var backgroundColor = Color.parseColor(rightBackgroundColor)
  35. private val clearPaint = Paint().apply { xfermode = PorterDuffXfermode(PorterDuff.Mode.CLEAR) }
  36.  
  37.  
  38. override fun onMove(p0: RecyclerView, p1: RecyclerView.ViewHolder, p2: RecyclerView.ViewHolder): Boolean {
  39. return false
  40. }
  41.  
  42. override fun onChildDraw(
  43. c: Canvas,
  44. recyclerView: RecyclerView,
  45. viewHolder: RecyclerView.ViewHolder,
  46. dX: Float,
  47. dY: Float,
  48. actionState: Int,
  49. isCurrentlyActive: Boolean
  50. ) {
  51.  
  52.  
  53. if (dX > 0) {
  54. icon = ContextCompat.getDrawable(context, leftIcon)
  55. backgroundColor = Color.parseColor(leftBackgroundColor)
  56. val itemView = viewHolder.itemView
  57. val itemHeight = itemView.bottom - itemView.top
  58. val isCanceled = dX == 0f && !isCurrentlyActive
  59.  
  60. if (isCanceled) {
  61. clearCanvas(
  62. c,
  63. itemView.left + dX,
  64. itemView.top.toFloat(),
  65. itemView.left.toFloat(),
  66. itemView.bottom.toFloat()
  67. )
  68. super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive)
  69. return
  70. }
  71.  
  72. // Draw the green check background
  73. background.color = backgroundColor
  74. background.setBounds(itemView.left + dX.toInt(), itemView.top, itemView.left, itemView.bottom)
  75. background.draw(c)
  76.  
  77. // Calculate position of delete icon
  78. val deleteIconTop = itemView.top + (itemHeight - intrinsicHeight) / DIVIDE_BY_TWO
  79. val deleteIconLeft = itemView.left + intrinsicWidth
  80. val deleteIconRight = itemView.left + DIVIDE_BY_TWO * intrinsicWidth
  81. val deleteIconBottom = deleteIconTop + intrinsicHeight
  82.  
  83. // Draw the delete icon
  84. icon?.setBounds(deleteIconLeft, deleteIconTop, deleteIconRight, deleteIconBottom)
  85. icon?.draw(c)
  86. } else {
  87. icon = ContextCompat.getDrawable(context, rightIcon)
  88. backgroundColor = Color.parseColor(rightBackgroundColor)
  89. val itemView = viewHolder.itemView
  90. val itemHeight = itemView.bottom - itemView.top
  91. val isCanceled = dX == 0f && !isCurrentlyActive
  92.  
  93. if (isCanceled) {
  94. clearCanvas(
  95. c,
  96. itemView.right + dX,
  97. itemView.top.toFloat(),
  98. itemView.right.toFloat(),
  99. itemView.bottom.toFloat()
  100. )
  101. super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive)
  102. return
  103. }
  104.  
  105. // Draw the red delete background
  106. background.color = backgroundColor
  107. background.setBounds(itemView.right + dX.toInt(), itemView.top, itemView.right, itemView.bottom)
  108. background.draw(c)
  109.  
  110. // Calculate position of delete icon
  111. val deleteIconTop = itemView.top + (itemHeight - intrinsicHeight) / DIVIDE_BY_TWO
  112. val deleteIconMargin = (itemHeight - intrinsicHeight) / DIVIDE_BY_TWO
  113. val deleteIconLeft = itemView.right - deleteIconMargin - intrinsicWidth
  114. val deleteIconRight = itemView.right - deleteIconMargin
  115. val deleteIconBottom = deleteIconTop + intrinsicHeight
  116.  
  117. // Draw the delete icon
  118. icon?.setBounds(deleteIconLeft, deleteIconTop, deleteIconRight, deleteIconBottom)
  119. icon?.draw(c)
  120. }
  121. super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive)
  122. }
  123.  
  124.  
  125. private fun clearCanvas(c: Canvas?, left: Float, top: Float, right: Float, bottom: Float) {
  126. c?.drawRect(left, top, right, bottom, clearPaint)
  127. }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement