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?

seekingStillness
  • 4,833
  • 5
  • 38
  • 68

3 Answers3

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
}
Calvin
  • 101
  • 3
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.

Cheticamp
  • 61,413
  • 10
  • 78
  • 131
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

    }
Ken Ratanachai S.
  • 3,307
  • 34
  • 43