Customize RecyclerView : Swipe Right/Left


Today I’m going show you the easiest way to implement recyclerview swiping right/left to perform operation like delete, archive, move, call, message, notify, or any suitable action you want to implement.
Step 1:
First, you have to setup Recyclerview in activity/fragment & RecyclerViewAdapter with ViewHolder.
Step 2:
Add the above code in your activity where you implemented recyclerview. Then call deleteTodoItem() method in onCreate().
All required code to implement swipe is in deleteTodoItem(). You can customize this method based on your needs…
Change Swipe Direction (Use both for single function or use swipe left for one functionality & swipe right to implement another one)
new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT)
Change background portion color after swiped
private ColorDrawable background = new ColorDrawable(Color.RED);
Icon after swiped
private Drawable icon = ContextCompat.getDrawable(getApplicationContext(),R.drawable.icon_delete);
There is a lot of customization we can do in it. Just play around & customize as per your need…
Step 3:
deleteTodoItem() method in TodoActivity.java calls deleteCurrentTodoItem() method from RecyclerViewAdapter TodoAdapter.java
If you're using some DB or any other functionality then appropriate action needed for data manipulation. I’ve used realmDb so, to delete item from Db I’ve used RealmTodoList.deleteFromRealm(position);
Otherwise write your own code to implement required functionality in deleteCurrentTodoItem() method.
That’s all you need to do. Easy peasy right? I hope you find something useful.
I’m an Android Developer & Designer. Checkout My Apps on Google Play & GitHub
If you like this article then please clap for me…👏👏👏