Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recycler swipe left and swipe right perform different actions

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?

like image 722
seekingStillness Avatar asked Mar 18 '17 13:03

seekingStillness


2 Answers

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
}
like image 187
Calvin Avatar answered Nov 16 '22 04:11

Calvin


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

like image 25
Cheticamp Avatar answered Nov 16 '22 03:11

Cheticamp