Thursday , August 16 2018
Home / Android / Android RecycleView Example

Android RecycleView Example

Google’s upcoming operating system named Android L looks very promising. It is highly focused on rich user experience and what they called it as material design. In this example we will take a look at the new UI widget called RecyclerView.

RecyclerView is more advanced and flexible and efficient version of ListView. RecyclerView ViewGroup is an container for larger data set of views that can be recycled and scrolled very efficiently. RecyclerView can be used for larger datasets to be rendered on the UI like a list. RecyclerView provides maximum flexibility to design different kind of views.

Support Older Versions

At the time of writing, less than 2% of Android devices run Android Lollipop. However, thanks to the v7 Support Library, you can use the RecyclerView and CardView widgets on devices that run older versions of Android by adding the following lines to the dependencies section in your project’s build.grade file:

compile ‘com.android.support:cardview-v7:21.0.+’
compile ‘com.android.support:recyclerview-v7:21.0.+’

Below is the RecyclerView widget with necessary attributes.

<android.support.v7.widget.RecyclerView
android:id=”@+id/recycler_view”
android:scrollbars=”vertical”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”/>

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

2. Open build.gradle and add recycler view dependency. com.android.support:recyclerview-v7:23.1.1 and rebuild the project.

build.gradle
dependencies {
compile fileTree(dir: ‘libs’, include: [‘*.jar’])
testCompile ‘junit:junit:4.12′
compile ‘com.android.support:appcompat-v7:23.1.1′
compile ‘com.android.support:design:23.1.1′
compile ‘com.android.support:recyclerview-v7:23.1.1′
}
3. With the latest version of build tools, Android Studio is creating two layout files for each activity. For main activity, it created activity_main.xml (contains CoordinatorLayout and AppBarLayout) and content_main.xml (for the actual content). Open content_main.xml and the recycler view widget.

content_main.xml

4. Open colors.xml located under res ⇒ values and add below colors.

colors.xml

 2. Writing the Adapter Class

After adding the RecyclerView widget, let’s start writing the adapter class to render the data. The RecyclerView adapter is same as ListView but the override methods are different.

5. Create a class named Movie.java and declare title, genre and year. Also add the getter/setter methods to each variable.

Movie.java

6. Create an layout xml named movie_list_row.xml with the below code. This layout file renders a single row in recycler view by displaying movie name, genre and year of release.

movie_list_row.xml

7. Now create a class named MoviesAdapter.java and add the below code. Here onCreateViewHolder() method inflates movie_list_row.xml. In onBindViewHolder() method the appropriate movie data (title, genre and year) set to each row.

MoviesAdapter.java

 

8. Now open MainActivity.java and do the below changes. Here prepareMovie() method adds sample data to list view.

MainActivity.java

Now if you run the app, you can see the movies displayed in a list manner.

android-recycler-view-example

3. Adding RecyclerView Divider / Separator

RecyclerView doesn’t have any divider related parameters to display the divider. Instead you need to extend a class from ItemDecoration and use addItemDecoration() method to display the divider.

9. Create a class named DividerItemDecoration.java and paste the below code.

DividerItemDecoration.java

10. Open MainActivity.java and set the item decoration using addItemDecoration() method before setting the adapter.

recyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));

// set the adapter
recyclerView.setAdapter(mAdapter);
Now if you run the app, you should see a divider line separating each row.

android-recycler-view-separator-divider

4. Adding RecyclerView Item Click Listener

RecyclerView doesn’t have OnItemClickListener method too to identify item click. You need to write your own class extending RecyclerView.OnItemTouchListener.

11. Open MainActivity.java and add the below RecyclerTouchListener class along with ClickListener interface.

MainActivity.java

Run the app and verify the item click. You should able to see a toast message upon clicking on a row. You can also notice that a background ripple effect when testing on lollipop device.

android-recycler-view-on-item-click-listener

Complete Code

Below is the complete code of my MainActivity.java

MainActivity.java

 

About admin

Check Also

Android Activity Launch Modes

Launch mode is an instruction for Android OS which specifies how the activity should be ...

Leave a Reply