How to Delete Multiple RecyclerView Items in Android?
RecyclerView is an advanced version of ListView with improved performance. When you have a long list of items to show you can use RecyclerView. It has the ability to reuse its views. In RecyclerView when the View goes out of the screen or is not visible to the user it won’t destroy it, it will reuse these views. This feature helps in reducing power consumption and providing more responsiveness to the application. In this article, we are going to see that how we can delete multiple items from a recycler view in android studio. Before moving further let us know about RecyclerView.
Before implementing the concept taught in this article, you should have the knowledge of implementing a recycler view in android studio. If you don’t know how to implement a recycler view then go through the following article: RecyclerView in Android with Example
What we are going to build in this article?
Here is a sample video of what we are going to implement in this article. Note that we are going to implement this application using Java language.
Step by Step Implementation
Step 1: Create a New Project
- Open a new project.
- We will be working on Empty Activity with language as Java. Leave all other options unchanged.
- You can change the name of the project at your convenience.
- There will be two default files named activity_main.xml and MainActivity.java.
If you don’t know how to create a new project in Android Studio then you can refer to How to Create/Start a New Project in Android Studio?
Step 2: Adding the required dependencies
Open Gradle Scripts > build.gradle(module). Go to app > right click > open module settings > dependencies > Add dependency > Library dependency.
Type material in the search bar and click on search. Select the dependency shown in the below image-
Type “lifecycle-extensions” in the search bar and click on search. Select the dependency shown in the below image-
Step 3: Add new vector assets in drawable
Navigate to drawable > right-click > new > vector asset and then select the following assets from clip art.
1. Check circle:
2. Select all:
Step 4: Working with xml files
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file.
Follow the path app > res > layout > right click > new > layout resource file and create a new file named as item_main.xml. Use the below code in item_main.xml file-
Follow the path app > res > right click > new > android resource file > Resource type > menu and create a new file named as menu.xml. Use the below code in menu.xml file-
Step 5: Working with java files
Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file. Comments are added inside the code to understand the code in more detail.
Follow the path app > java > com.example.multiple_item_delete > right click > new > java class and create a new file named as MainAdapter.java. Use the below code in MainAdapter.java file-
Follow the path app > java > com.example.multiple_item_delete > right-click > new > java class and create a new file named as MainViewModel.java. Use the below code in MainViewModel.java file-
Here is the final output of our application.