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?
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
}
Make use of the direction
argument to onSwiped()
. See the documentation. You can make adjustments depending upon whether you are swiping right or left.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With