Advertisement
Guest User

Untitled

a guest
May 12th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 1.74 KB | None | 0 0
  1. abstract class SwipeToDeleteCallback(context: Context) : ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT) {
  2.  
  3.     private val deleteIcon = ContextCompat.getDrawable(context, R.drawable.ic_delete_white)
  4.     private val intrinsicWidth = deleteIcon.intrinsicWidth
  5.     private val intrinsicHeight = deleteIcon.intrinsicHeight
  6.     private val background = ColorDrawable()
  7.     private val backgroundColor = Color.parseColor("#f44336")
  8.  
  9.     override fun onMove(recyclerView: RecyclerView?, viewHolder: RecyclerView.ViewHolder?, target: RecyclerView.ViewHolder?): Boolean {
  10.         return false
  11.     }
  12.  
  13.     override fun onChildDraw(c: Canvas?, recyclerView: RecyclerView?, viewHolder: RecyclerView.ViewHolder, dX: Float, dY: Float, actionState: Int, isCurrentlyActive: Boolean) {
  14.         val itemView = viewHolder.itemView
  15.         val itemHeight = itemView.bottom - itemView.top
  16.  
  17.         // Draw the red delete background
  18.         background.color = backgroundColor
  19.         background.setBounds(itemView.right + dX.toInt(), itemView.top, itemView.right, itemView.bottom)
  20.         background.draw(c)
  21.  
  22.         // Calculate position of delete icon
  23.         val deleteIconTop = itemView.top + (itemHeight - intrinsicHeight) / 2
  24.         val deleteIconMargin = (itemHeight - intrinsicHeight) / 2
  25.         val deleteIconLeft = itemView.right - deleteIconMargin - intrinsicWidth
  26.         val deleteIconRight = itemView.right - deleteIconMargin
  27.         val deleteIconBottom = deleteIconTop + intrinsicHeight
  28.  
  29.         // Draw the delete icon
  30.         deleteIcon.setBounds(deleteIconLeft, deleteIconTop, deleteIconRight, deleteIconBottom)
  31.         deleteIcon.draw(c)
  32.  
  33.         super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive)
  34.     }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement