Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- abstract class SwipeToDeleteCallback(context: Context) : ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT) {
- private val deleteIcon = ContextCompat.getDrawable(context, R.drawable.ic_delete_white)
- private val intrinsicWidth = deleteIcon.intrinsicWidth
- private val intrinsicHeight = deleteIcon.intrinsicHeight
- private val background = ColorDrawable()
- private val backgroundColor = Color.parseColor("#f44336")
- override fun onMove(recyclerView: RecyclerView?, viewHolder: RecyclerView.ViewHolder?, target: RecyclerView.ViewHolder?): Boolean {
- return false
- }
- override fun onChildDraw(c: Canvas?, recyclerView: RecyclerView?, viewHolder: RecyclerView.ViewHolder, dX: Float, dY: Float, actionState: Int, isCurrentlyActive: Boolean) {
- val itemView = viewHolder.itemView
- val itemHeight = itemView.bottom - itemView.top
- // Draw the red delete background
- background.color = backgroundColor
- background.setBounds(itemView.right + dX.toInt(), itemView.top, itemView.right, itemView.bottom)
- background.draw(c)
- // Calculate position of delete icon
- val deleteIconTop = itemView.top + (itemHeight - intrinsicHeight) / 2
- val deleteIconMargin = (itemHeight - intrinsicHeight) / 2
- val deleteIconLeft = itemView.right - deleteIconMargin - intrinsicWidth
- val deleteIconRight = itemView.right - deleteIconMargin
- val deleteIconBottom = deleteIconTop + intrinsicHeight
- // Draw the delete icon
- deleteIcon.setBounds(deleteIconLeft, deleteIconTop, deleteIconRight, deleteIconBottom)
- deleteIcon.draw(c)
- super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement