Created
January 24, 2018 19:21
-
-
Save kitek/33e85cbc3b9fadbc303ff5b78cb50ecc to your computer and use it in GitHub Desktop.
Example demonstrating how to disable swipe to delete for the particular item.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package pl.kitek.rvswipetodelete | |
import android.content.Context | |
import android.graphics.Canvas | |
import android.graphics.Color | |
import android.graphics.drawable.ColorDrawable | |
import android.support.v4.content.ContextCompat | |
import android.support.v7.widget.RecyclerView | |
import android.support.v7.widget.helper.ItemTouchHelper | |
abstract class SwipeToDeleteCallback(context: Context) : ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT) { | |
private val deleteIcon = ContextCompat.getDrawable(context, R.drawable.ic_delete_white_24) | |
private val intrinsicWidth = deleteIcon.intrinsicWidth | |
private val intrinsicHeight = deleteIcon.intrinsicHeight | |
private val background = ColorDrawable() | |
private val backgroundColor = Color.parseColor("#f44336") | |
override fun getMovementFlags(recyclerView: RecyclerView?, viewHolder: RecyclerView.ViewHolder?): Int { | |
// Check here whatever you want, return 0 if you want disable swipe. | |
//if (viewHolder?.itemViewType == YourAdapter.SOME_TYPE) return 0 | |
if (viewHolder?.adapterPosition == 0) return 0 | |
return super.getMovementFlags(recyclerView, viewHolder) | |
} | |
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) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you