Skip to content

Commit

Permalink
added swipe direction with callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
pknotfound committed Apr 23, 2021
1 parent c960435 commit 6388881
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,15 @@ fun RecyclerView.addSwipeToDelete(
}

override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
listener?.swipeToDelete(adapterPosition = viewHolder.adapterPosition)
val swipeDirection: DIRECTION = when(direction) {
ItemTouchHelper.UP -> DIRECTION.UP
ItemTouchHelper.DOWN -> DIRECTION.DOWN
ItemTouchHelper.LEFT -> DIRECTION.LEFT
else -> DIRECTION.RIGHT
}
listener?.swipeToDelete(adapterPosition = viewHolder.adapterPosition,
swipeDirection = swipeDirection
)
this@addSwipeToDelete.adapter?.notifyItemRemoved(viewHolder.adapterPosition)
}

Expand Down Expand Up @@ -81,6 +89,6 @@ private fun Float.isPositive(): Boolean {
}

interface OnSwiped {
fun swipeToDelete(adapterPosition: Int)
fun swipeToDelete(adapterPosition: Int, swipeDirection: DIRECTION)
}

Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,13 @@ fun RecyclerView.addSwipeToPerform(
}

override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
listener?.swipeToPerform(adapterPosition = viewHolder.adapterPosition)
val swipeDirection: DIRECTION = when(direction) {
ItemTouchHelper.UP -> DIRECTION.UP
ItemTouchHelper.DOWN -> DIRECTION.DOWN
ItemTouchHelper.LEFT -> DIRECTION.LEFT
else -> DIRECTION.RIGHT
}
listener?.swipeToPerform(adapterPosition = viewHolder.adapterPosition,swipeDirection = swipeDirection)
}

override fun onChildDraw(
Expand Down Expand Up @@ -75,5 +81,5 @@ fun RecyclerView.addSwipeToPerform(
}

interface OnSwipeToPerform {
fun swipeToPerform(adapterPosition: Int)
fun swipeToPerform(adapterPosition: Int, swipeDirection: DIRECTION)
}
10 changes: 5 additions & 5 deletions app/src/main/java/com/kedia/customswipelibrary/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class MainActivity : AppCompatActivity(), OnSwiped, OnDragged, OnSwipeToPerform
val list = listOf(
DIRECTION.LEFT,
DIRECTION.RIGHT)
recycler.addSwipeToPerform(list, this, ContextCompat.getColor(this, R.color.colorPrimaryDark))
recycler.addSwipeToDelete(list, this, ContextCompat.getColor(this, R.color.colorPrimaryDark))

/**
recycler.addSwipeToPerform(list, this, ContextCompat.getColor(this, R.color.colorPrimaryDark))
Expand All @@ -49,13 +49,13 @@ class MainActivity : AppCompatActivity(), OnSwiped, OnDragged, OnSwipeToPerform
adapter.moveItem(fromPosition, toPosition)
}

override fun swipeToDelete(adapterPosition: Int) {
Log.d("TAG!!!!", "swipeToDelete: ")
override fun swipeToDelete(adapterPosition: Int, swipeDirection: DIRECTION) {
Log.d("TAG!!!!", "swipeToDelete: $swipeDirection")
adapter.removeItem(adapterPosition)
}

override fun swipeToPerform(adapterPosition: Int) {
Log.d("TAG!!!!", "swipeToPerform: swiped")
override fun swipeToPerform(adapterPosition: Int, swipeDirection: DIRECTION) {
Log.d("TAG!!!!", "swipeToPerform: swiped $swipeDirection")
}

}

0 comments on commit 6388881

Please sign in to comment.