Recycler swipe left and swipe right perform different actions
Asked Answered
S

3

8

I have the following recycler view code that that allows the user to swipe right (deleting card from view and also deleting data from SQLite db) and also allowing the user to move tags to rearrange. It works. I'm happy.

ItemTouchHelper.SimpleCallback simpleItemTouchCallback = new ItemTouchHelper.SimpleCallback(ItemTouchHelper.UP | ItemTouchHelper.DOWN,  ItemTouchHelper.RIGHT) {
        @Override
        public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
            //awesome code when user grabs recycler card to reorder   
        } 

        @Override
        public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
            super.clearView(recyclerView, viewHolder);
            //awesome code to run when user drops card and completes reorder
        }

        @Override
        public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
            //awesome code when swiping right to remove recycler card and delete SQLite data
        }
    };
    ItemTouchHelper itemTouchHelper = new ItemTouchHelper(simpleItemTouchCallback);
    itemTouchHelper.attachToRecyclerView(recyclerView);

Now, I want to add the ability to swipe LEFT and perform something different from swiping RIGHT. All the solutions I've found so far would require a rewrite of my code. Any suggestions to add SWIPE LEFT to my existing code?

Spencerspencerian answered 18/3, 2017 at 13:58 Comment(0)
M
7

On onSwiped method, add-

if (direction == ItemTouchHelper.RIGHT) {
//whatever code you want the swipe to perform
}

then another one for left swipe-

if (direction == ItemTouchHelper.LEFT) {
//whatever code you want the swipe to perform
}
Melessa answered 18/3, 2017 at 14:51 Comment(0)
G
1

Make use of the direction argument to onSwiped(). See the documentation. You can make adjustments depending upon whether you are swiping right or left.

Galleass answered 18/3, 2017 at 14:5 Comment(0)
C
0

For me, direction returns START/END not LEFT/RIGHT, so this is what i have to do.

    if(direction == ItemTouchHelper.START) {
        // DO Action for Left

    } else if(direction == ItemTouchHelper.END) {
        // DO Action for Right

    }
Cutright answered 2/2, 2018 at 5:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.