Skip to content

Instantly share code, notes, and snippets.

@rye761
Created October 23, 2018 22:29
Show Gist options
  • Save rye761/fbc692110319701573e9660b5a0e7175 to your computer and use it in GitHub Desktop.
Save rye761/fbc692110319701573e9660b5a0e7175 to your computer and use it in GitHub Desktop.
package heatwave.ryanaird.com.swipyrecyclerview
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.Drawable
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v4.content.ContextCompat
import android.support.v7.widget.DividerItemDecoration
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.support.v7.widget.helper.ItemTouchHelper
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
private lateinit var viewAdapter: RecyclerView.Adapter<*>
private lateinit var viewManager: RecyclerView.LayoutManager
private lateinit var colorDrawableBackground: ColorDrawable
private lateinit var deleteIcon: Drawable
private var dataset = mutableListOf("Chicken", "Fish", "Beef", "Pork", "Lamb")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
viewAdapter = MainAdapter(dataset)
viewManager = LinearLayoutManager(this)
colorDrawableBackground = ColorDrawable(Color.parseColor("#ff0000"))
deleteIcon = ContextCompat.getDrawable(this, R.drawable.ic_delete)!!
recyclerview.apply {
setHasFixedSize(true)
adapter = viewAdapter
layoutManager = viewManager
addItemDecoration(DividerItemDecoration(this.context, DividerItemDecoration.VERTICAL))
}
val itemTouchHelperCallback = object : ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT) {
override fun onMove(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder, viewHolder2: RecyclerView.ViewHolder): Boolean {
return false
}
override fun onSwiped(viewHolder: RecyclerView.ViewHolder, swipeDirection: Int) {
(viewAdapter as MainAdapter).removeItem(viewHolder.adapterPosition, viewHolder)
}
override fun onChildDraw(
c: Canvas,
recyclerView: RecyclerView,
viewHolder: RecyclerView.ViewHolder,
dX: Float,
dY: Float,
actionState: Int,
isCurrentlyActive: Boolean
) {
val itemView = viewHolder.itemView
val iconMarginVertical = (viewHolder.itemView.height - deleteIcon.intrinsicHeight) / 2
if (dX > 0) {
colorDrawableBackground.setBounds(itemView.left, itemView.top, dX.toInt(), itemView.bottom)
deleteIcon.setBounds(itemView.left + iconMarginVertical, itemView.top + iconMarginVertical,
itemView.left + iconMarginVertical + deleteIcon.intrinsicWidth, itemView.bottom - iconMarginVertical)
} else {
colorDrawableBackground.setBounds(itemView.right + dX.toInt(), itemView.top, itemView.right, itemView.bottom)
deleteIcon.setBounds(itemView.right - iconMarginVertical - deleteIcon.intrinsicWidth, itemView.top + iconMarginVertical,
itemView.right - iconMarginVertical, itemView.bottom - iconMarginVertical)
deleteIcon.level = 0
}
colorDrawableBackground.draw(c)
c.save()
if (dX > 0)
c.clipRect(itemView.left, itemView.top, dX.toInt(), itemView.bottom)
else
c.clipRect(itemView.right + dX.toInt(), itemView.top, itemView.right, itemView.bottom)
deleteIcon.draw(c)
c.restore()
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive)
}
}
val itemTouchHelper = ItemTouchHelper(itemTouchHelperCallback)
itemTouchHelper.attachToRecyclerView(recyclerview)
}
}
package heatwave.ryanaird.com.swipyrecyclerview
import android.support.constraint.ConstraintLayout
import android.support.design.widget.Snackbar
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
class MainAdapter(private val dataset: MutableList<String>) : RecyclerView.Adapter<MainAdapter.MainViewHolder>() {
private var removedPosition: Int = 0
private var removedItem: String = ""
class MainViewHolder(v: View) : RecyclerView.ViewHolder(v) {
val title: TextView = v.findViewById(R.id.title)
}
override fun onCreateViewHolder(viewGroup: ViewGroup, viewType: Int): MainAdapter.MainViewHolder {
val v = LayoutInflater.from(viewGroup.context)
.inflate(R.layout.row, viewGroup, false)
return MainViewHolder(v)
}
override fun onBindViewHolder(viewHolder: MainViewHolder, position: Int) {
viewHolder.title.text = dataset[position]
}
fun removeItem(position: Int, viewHolder: RecyclerView.ViewHolder) {
removedItem = dataset[position]
removedPosition = position
dataset.removeAt(position)
notifyItemRemoved(position)
Snackbar.make(viewHolder.itemView, "$removedItem removed", Snackbar.LENGTH_LONG).setAction("UNDO") {
dataset.add(removedPosition, removedItem)
notifyItemInserted(removedPosition)
}.show()
}
override fun getItemCount() = dataset.size
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment