Customize RecyclerView : Swipe Right/Left

Shubham Nikam
2 min readJul 6, 2019

--

Implementation of Swipe Right/Left to delete the item

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…👏👏👏

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

No responses yet

Write a response