1

I am trying to build a swipe option in RecyclerView item. The problem is that, When I swipe to the right the whole item is gone. This is not how I want it to be.

Current Issue




This is the example which I want the swipe action to work. When the user swipes to right after particular threshold I want the swipe width to stop and when the user release, I want it to go back.

Currently the user can swipe to the right more than a particular width as shown in the first gif file. I want to limit that and make sure that the user can only swipe to a certain limit and when the user releases it goes back.



Needed

Current Code:

  val myCallback = object: ItemTouchHelper.SimpleCallback(0,
        ItemTouchHelper.RIGHT) {
        override fun onMove(
            recyclerView: RecyclerView,
            viewHolder: RecyclerView.ViewHolder,
            target: RecyclerView.ViewHolder
        ): Boolean = false

        override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {

        }

        override fun onChildDraw(
            c: Canvas,
            recyclerView: RecyclerView,
            viewHolder: RecyclerView.ViewHolder,
            dX: Float,
            dY: Float,
            actionState: Int,
            isCurrentlyActive: Boolean
        ) {


            super.onChildDraw(c, recyclerView, viewHolder,
                dX , dY , actionState, isCurrentlyActive)

            c.clipRect(0f, viewHolder.itemView.top.toFloat(),
                dX , viewHolder.itemView.bottom.toFloat())

            if(dX < c.width / 3)
                c.drawColor(Color.GRAY)
            else
                c.drawColor(Color.RED)

            val textMargin = resources.getDimension(R.dimen.text_margin)
                .roundToInt()
            trashBinIcon.bounds = Rect(
                textMargin,
                viewHolder.itemView.top + textMargin,
                textMargin + trashBinIcon.intrinsicWidth,
                viewHolder.itemView.top + trashBinIcon.intrinsicHeight
                        + textMargin
            )
            trashBinIcon.draw(c)
        }


    }

I tried ItemTouchHelper : Limit swipe width of ItemTouchHelper.SimpleCallBack on RecyclerView solution but didn't work.

Jake Lee
  • 7,549
  • 8
  • 45
  • 86
pepperlove
  • 170
  • 14
  • Did you try this?: https://stackoverflow.com/a/48944909/9715339 – Mayur Gajra Jul 14 '21 at 15:41
  • Yes. I tried it, but was not working. – pepperlove Jul 14 '21 at 16:53
  • I found the answer for the first problem. Just need to reduce the width of canvas `c.clipRect(0f, viewHolder.itemView.top.toFloat(), dX / 4, viewHolder.itemView.bottom.toFloat())`. But the second issue still there, After the threshold the item should slide back. – pepperlove Jul 14 '21 at 17:56

0 Answers0