Home / UI / Recyclerview / Android Recyclerview with custom adapter – Tutorial
Android Recyclerview with custom adapter

Android Recyclerview with custom adapter – Tutorial

Android Recyclerview with custom adapter

The Recyclerview widget was introduced in Android Support Library package and it 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. In this tutorial we will make Android Recyclerview with custom adapter app that would display a list of Movies which has a title, rating and an image in a recyclerview, also we’ll implement RecyclerView.OnItemTouchListener to add touch event to every single row in the recyclerview.

Screenshot of the Android Recyclerview with custom adapter final product:

Android Recyclerview with custom adapter

To make it works, you need to set a LayoutManager, a java class representing the data model and an adapter to link between them.

RecyclerView

1. Creating Android Project, In Android Studio, create a new project by navigating to File ⇒ New Project and fill all the required details. When it prompts to select a default activity, select Blank Activity and proceed.

2. We need to add the Android Support Library package, to do that open build.gradle located under app folder and add Android Support Library dependency.  So it will looks like that:

3. Let’s add some styles, Open colors.xml under res ⇒ values and add below color resources. If you don’t find colors.xml, create a new file with the name.

And Open styles.xml under res ⇒ values and add below color resources.

4. Now under your project’s package, create three packages named activity, adapter and data. Drag your MainActivity.java file into activity package.

5. Under data package, create a java class named Movie.java and add below code. This is the model class required to create movie objects to provide data to the ListView

6. Now let’s create a custom adapter class for our list view. Under res ⇒ layout folder, create an xml layout named single_movie_row.xml. This xml renders single list row in the ListView.

7. To implement the Android Recyclerview with custom adapter, we need to implement the adapter class, under adapter package, create another class named MovieRecyclerviewAdapter.java. This class is a custom adapter class which inflates the single_movie_row.xml by applying proper data.

 

8. Now we are ready to use these files in our main activity, let’s start implementing the Recyclerview. Open the layout file of your main activity (activity_main.xml) and modify the layout as shown below. It will just has a recyclerview and a ToolBar.

9. Finally open MainActivity.java and do the below changes to achieve the Android Recyclerview with custom adapter.

  • Instantiate and get a reference of the recyclerview we put in activity_main.xml and setLayoutManager for this recyclerview
  • Instantiate our custom adapter and make an array of Movie objects
  • Make a class RecyclerTouchListener which implements RecyclerView.OnItemTouchListener
  • Download images used in the demo from here, extract the .zip file Under res
 

10. Run the project and test it. You should able see the Android Recyclerview with custom adapter as seen in the screenshot of the final product.

Download Source Code

 

 

Summary
Article Title
Android Recyclerview with custom adapter - Tutorial
Description
Android Recyclerview with custom adapter app that displays a list of Movies with an image and implement RecyclerView.OnItemTouchListener to add touch events
Author
Organization
Android Gifts

About #AndroidGifts

#AndroidGifts

Check Also

featured_image

Material Design Icons with Android Studio Plugin Drawable Importer – Library #1

In this tutorial we are having Material Design Icons with Android Studio Drawable Importer Plugin which …

Leave a Reply