5

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. enter image description here

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 enter image description here

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.

Nimdokai
  • 787
  • 1
  • 6
  • 18
  • +1 for the sample code! Unfortunately, I can't reproduce the error on an emulator with API 23. Did you test on an emulator? Which one? – Bö macht Blau Dec 05 '19 at 20:30
  • I've checked on 2x emulators API 28 with different resolutions, also on phone. What `transitionX` you've set? As I stated in code with ~0.4f it seems to work, but above I am starting to see this bug. – Nimdokai Dec 05 '19 at 20:36

1 Answers1

-1

Did you try AndroidSwipeLayout? There is already a pretty good library for this, which I also use in my app. https://github.com/daimajia/AndroidSwipeLayout

Bio-Matic
  • 793
  • 1
  • 8
  • 20
  • Hi, thank you for suggestion. I've seen that library. I didn't want to use it because of dependency that it's adds on adapter, though I'll consider it if it will not work. – Nimdokai Dec 05 '19 at 20:34