RecyclerView long-click to remove an item

In this series of recyclerView tutorials, we’re going to see how to use a long-click to remove an item from recyclerView.

Before that please visit the previous tutorials on what is RecyclerView and How to implement recyclerView using CardView in android.

There are many ways we can remove an item from recyclerView. In this series, we will discuss every possible way we could delete or remove an item from recyclerView. Today we will see a long-click to remove an item.

Before that, we will see some of the other stuff like, how to add a ripple effect on an itemView and how to set divider item decoration in recyclerView.

Let’s discuss three of them below

1. First, add the RecyclerView dependency

2. Ripple effect on itemView

We just need to add a line in the layout file of the item which represents the item view for the recyclerView.

The above attribute will give a ripple effect on the item of the recyclerView.

Example:

3. Set Divider item decoration in recyclerView

Here we set a divider vertically below each item of the recyclerView.

4. Now. Let’s remove the item from recyclerView with long-click

For removing an item from recyclerView with a long click, set the long click listener on the itemView.

notifyItemRemoved(getAdapterPosition()), it removed the item from recyclerView.

Example:

You can also, use the below codes and run it into your projects.

activity_main.xml

row_item.xml

RecyclerViewAdapter.java

MainActivty.java

For more visit www.gbandroidblogs.com

and follow my page here. Click on the clap button and show some love.

Happy Coding!!

Hi everyone, myself Golap an Android app developer with UI/UX designer.