How To add swipe Left to Right in recyclerview
Asked Answered
S

1

4

I am trying to add swipe function for my recyclerview. I am following this link for adding swipe. https://github.com/nikhilpanju/RecyclerViewEnhanced/blob/master/recyclerviewenhanced/src/main/java/com/nikhilpanju/recyclerviewenhanced/RecyclerTouchListener.java

In this link I can do only Right to left swipe. I want add Left to Right swipe. I tried to add functionality in onInterceptTouchEvent. But I can not do the Left to Right Swipe. Can any one help me to add Left to Right swipe?

Stagg answered 15/10, 2020 at 7:38 Comment(0)
B
5

To implement such behavior in a RecyclerView, you need to declare an ItemTouchHelper, like so:

// The class to detect swipes and drags
private lateinit var itemTouchHelper: ItemTouchHelper

The ItemTouchHelper class has a ItemTouchHelper.SimpleCallback.onChildDraw callback method, where you can detect if a user swept right or left on an item in your recyclerview. So next, we'll now implement this callback, like so:

private fun setupRecyclerView() {
    val myRecyclerView = findViewById<RecyclerView>(R.id.myRecyclerView)
    val customAdapter = CustomAdapter()
    myRecyclerView.adapter = customAdapter

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

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

           // If you want to add a background, a text, an icon
          //  as the user swipes, this is where to start decorating
          //  I will link you to a library I created for that below

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

        override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
            val position = viewHolder.adapterPosition
            when (direction) {
                ItemTouchHelper.LEFT -> {
                   // Do something when a user swept left
                }
                ItemTouchHelper.RIGHT -> {
                  // Do something when a user swept right
                }
            }
        }
    }
    itemTouchHelper = ItemTouchHelper(simpleCallback)
    itemTouchHelper.attachToRecyclerView(myRecyclerView)
}

Now that swipe behavior will now be listened in your recyclerview. Also, in case you want a background, icon or text to be shown as the user of your app swipes, you can use this awesome library I created just for that usage. you can download it to your project if you're using gradle. Here's the link to the documentation for it: https://github.com/kevingermainbusiness/ItemDecorator

add to favorites swipe to delete

Bonefish answered 26/4, 2021 at 21:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.