I am trying to implement swipe for RecyclerView
's items.
Implementation is mostly based on this.
Background:
When user swipes view, it's moved to the correct position, but it does not disappear. - OK
Example: below user swiped item 2. The Red square is a background behind the view.
Then: When user tap (not swipe) the swiped item the escape animation is triggered (default one). - OK
Issue: The view, does not go back fully to the default position. User is able to see part of the background on the right side of the screen (as below).
It requires second tap to set the view in correct position. - NOT OK
To better demonstrate behavior I've created a GIF.
Code regarding the ItemTouchHelper.SimpleCallback
:
class SwipeCallback : ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT) {
override fun onMove(recyclerView: RecyclerView, viewHolder: ViewHolder, target: ViewHolder) =
false
override fun onChildDraw(
canvas: Canvas,
recyclerView: RecyclerView,
viewHolder: ViewHolder,
dX: Float,
dY: Float,
actionState: Int,
active: Boolean
) {
if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {
// SWIPE LEFT
if (dX < 0) {
// When transitionX is multiply by ~0.4f or lower value, view is displayed properly.
// Otherwise when user will "tap" the background will be shown.
// On the other hand if we do "proper" swipe to escape view will be displayed properly.
val transitionX = 0.7f * dX
super.onChildDraw(
canvas,
recyclerView,
viewHolder,
transitionX,
dY,
actionState,
active
)
}
}
}
override fun onSwiped(viewHolder: ViewHolder, direction: Int) {
}
}
Sample project to demonstrate if somebody is interested.