Last active
February 15, 2018 17:02
-
-
Save vorobeij/fa35024ff98f07abab8a7266faf904e0 to your computer and use it in GitHub Desktop.
Swipe to delete callback
This file contains hidden or 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
| private fun initRvActions() { | |
| rvCommands.layoutManager = LinearLayoutManager(this) | |
| rvCommands.adapter = AdapterCommands(model) | |
| rvCommands.addItemDecoration(DividerItemDecoration(this, DividerItemDecoration.VERTICAL)) | |
| val swipeHandler = object : SwipeToDeleteCallback(this) { | |
| override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) { | |
| val adapter = rvCommands.adapter as AdapterCommands | |
| adapter.removeAt(viewHolder.adapterPosition) | |
| } | |
| override fun getMovementFlags(recyclerView: RecyclerView?, | |
| viewHolder: ViewHolder?): Int { | |
| Timber.d("jsp pos=${viewHolder?.adapterPosition}") | |
| // dont allow remove first element | |
| if (viewHolder?.adapterPosition == 0) return 0 | |
| return (makeFlag(ACTION_STATE_IDLE, ItemTouchHelper.LEFT) or | |
| makeFlag(ACTION_STATE_SWIPE, ItemTouchHelper.LEFT)) | |
| } | |
| } | |
| val itemTouchHelper = ItemTouchHelper(swipeHandler) | |
| itemTouchHelper.attachToRecyclerView(rvCommands) | |
| } |
This file contains hidden or 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
| import android.content.Context | |
| import android.graphics.Bitmap | |
| import android.graphics.Bitmap.Config | |
| import android.graphics.Canvas | |
| import android.graphics.Color | |
| import android.graphics.drawable.BitmapDrawable | |
| import android.graphics.drawable.ColorDrawable | |
| import android.provider.MediaStore | |
| import android.support.v4.content.ContextCompat | |
| import android.support.v7.widget.RecyclerView | |
| import android.support.v7.widget.helper.ItemTouchHelper | |
| import timber.log.Timber | |
| abstract class SwipeToDeleteCallback(val 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") | |
| private val canvas = Canvas() | |
| 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 | |
| val backgroundWidth = -dX.toInt() | |
| val backgroundLeft = itemView.right + dX.toInt() | |
| // 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 | |
| val cCopy = Bitmap.createBitmap(c.width, c.height, Config.ARGB_4444) | |
| canvas.setBitmap(cCopy) | |
| // Draw the red delete background | |
| background.color = backgroundColor | |
| background.setBounds(backgroundLeft, itemView.top, itemView.right, itemView.bottom) | |
| background.draw(canvas) | |
| // Draw the delete icon | |
| deleteIcon!!.setBounds(deleteIconLeft, deleteIconTop, deleteIconRight, deleteIconBottom) | |
| deleteIcon!!.draw(canvas) | |
| val dr = BitmapDrawable(context.resources, cCopy) | |
| val allIconTop = itemView.top | |
| val allIconLeft = backgroundLeft | |
| val allIconRight = itemView.right | |
| val allIconBottom = itemView.bottom | |
| if (backgroundWidth > 0) { | |
| val hiddenAreaBmp = Bitmap.createBitmap(dr.bitmap, allIconLeft, allIconTop, backgroundWidth, itemHeight) | |
| val hidden = BitmapDrawable(context.resources, hiddenAreaBmp) | |
| hidden.setBounds(allIconLeft, allIconTop, allIconRight, allIconBottom) | |
| hidden.draw(c) | |
| } | |
| super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive) | |
| } | |
| private fun saveBitmap(bitmap: Bitmap, | |
| name: String) { | |
| val s = context.packageName | |
| Timber.d("jsp bitmap is saved ") | |
| MediaStore.Images.Media.insertImage(context.contentResolver, bitmap, name, s); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment