Android Tutorials : RecyclerView Implementation on Android

RecyclerView In Android

The Android RecyclerView is a more advanced and flexible version of ListView. This widget is a container for displaying large data sets that can be scrolled very efficiently by maintaining a limited number of views. Use the RecyclerView widget when you have data collections whose elements change at runtime based on user action or network events.
To use the RecyclerView widget, you have to specify an adapter and a layout manager. To create an adapter, extend the RecyclerView.Adapter class. A layout manager positions item views inside a RecyclerView and determines when to reuse item views that are no longer visible to the user. To reuse (or recycle) a view, a layout manager may ask the adapter to replace the contents of the view with a different element from the dataset. Recycling views in this manner improves performance by avoiding the creation of unnecessary views or performing expensive findViewById() lookups.

The RecyclerView class simplifies the display and handling of large data sets by providing:

  • Layout managers for positioning items
  • Default animations for common item operations, such as removal or addition of items

RecyclerView provides these built-in layout managers:

  • LinearLayoutManager shows items in a vertical or horizontal scrolling list.
  • GridLayoutManager shows items in a grid.
  • StaggeredGridLayoutManager shows items in a staggered grid.

To create a custom layout manager,you can extend the RecyclerView.LayoutManager class. We will be using LinearLayoutManager in this tutorial.

RecyclerView Implementation in an Android application project

Some obvious steps : 

  1. Create a new Android Project using Android Studio. Give it a name and select the Minimum SDK on which your app will run on. I chose API 15 : Android 4.0.3 (IceCreamSandwich).
  2. When you are prompted to add an activity to your application choose Blank Activity and click on next button.
  3. In the next step click on Finish and in few seconds your application should be loaded in Android Studio.

Now here are the steps for the modifications in build file and XML files of your application.

1. Open build.gradle(Module:App) file of your application and add the following dependencies in the dependencies section of build.gradle file.The buildToolsVersion used in this project is 23.0.2 as specified in the build.gradle file.

2. Add the following code to activity_main.xml file of your application. You can modify if you want to have different layout for your application.

3. Create a new layout resource file under the layout directory of your application and name it as main_fragment.xml and add the following code in it.

4. Create a new layout resource file under the layout directory of your application and name it as place_list_item.xml and add the following code in it.

5. The colors.xml file of the application looks like this.

6. The styles.xml file of the application looks like this.

7. Also add all the required icons or images in drawable folder of your application. You can download the project from the link given at the end of this post and copy the files in drawable folder of it and paste it in the drawable folder of your application.

Now here are the modifications in MainActivity.java and other java files of your application.

1. Open MainActivity.java file and make the following changes in it.

2. Create a new Java class under your default package and name it as MainFragment.java. MainFragment.java has the following code in it.

3. Create a new Java class under your default package and name it as Place.java. Place.java has the following code in it.

4. Create a new Java class under your default package and name it as PlaceAdapter.java. PlaceAdapter.java has the following code in it.

5. Create a new Java interface under your default package and name it as RecyclerViewOnClickListener.java. RecyclerViewOnClickListener.java has the following code in it.

6. The AndroidManifest.xml file of the application looks like this.

That’s it!. You have added a RecyclerView in your application. If you have followed all the steps you can now Run your application and test how it works. The following image shows the RecyclerView generated in this android application.

Android RecyclerView

Download Project

RecyclerView Implementation on Android

Leave a Reply

Your email address will not be published. Required fields are marked *