Android RecyclerView with Dividers

In this tutorial

Here we learn how to render android RecyclerView widgit with custom layout to show list of android versions and how to use RecyclerView.ItemDecoration to show recyclerview with dividers. Also here we are going to look the drawback of implementing default RecyclerView.ItemDecoration to show divider and learn how to implement our own DividerItemDecoration by extending RecyclerView.ItemDecoration. In the end it’s looking like this.

recyclerview


Download

The source code for this post is available at GitHub. You can download/checkout this from here


What is RecyclerView?

The android recyclerView is a new ViewGroup for displaying collections. RecyclerView can be thought of as a replacement for the ListView and GridView widgets in Android. Like its predecessors, RecyclerView is designed to display a large data set in a small window, but RecyclerView offers more layout options and is better optimized. It provides the ability to implement both horizontal and vertical layouts. Using this single widgit you are able to displays a collection of items in a list or a grid, enabling the user to scroll through the collection.

In Android RecyclerView was added from lollipop version 22.1.0 and is offered as a support library. RecyclerView works with apps that target API level 7 (Android 2.1) and later. You can learn about RecyclerView in detail from here

Adding Android RecyclerView Widget

1. build.gradle

Adding support library dependency for RecyclerView

2. activity_main.xml

In my main layout file, I have added android RecyclerView Widget. I had also added some extra attributes here for showing scrollbar while scrolling. These attribute are not compulsory to use.

Model Class

1. AndroidVersion.java

A model class is created to hold specific android version data.

Row Layout

1. item_list.xml

To render recyclerView single row.

Adapter and ViewHolder

1. Adapter

For recyclerView, an adapter class must have to implement RecyclerView.Adapter. RecyclerView.Adapter – handle the data collection and bind it to the view. Also with a RecyclerView the adapter requires the existence of a ViewHolder object which describes and provides access to all the views within each item row.

2. ViewHolder


RecyclerView enhances performance by recycling views and by enforcing the view-holder pattern, which eliminates unnecessary layout resource lookups. Use of the view-holder pattern is optional in ListView. Every ViewHolder class must need to extends RecyclerView.ViewHolder.

3. ListAdapter.java

An adapter class to show Android Versions list

Activity

Open your main activity and do the below changes. Here we are going set an adapter to our recyclerView. Here getVersionsList method is used to prepare dummy android versions list.

1. MainActivity.java

RecyclerView Divider

1. Default Divider

You can add the divider line between rows by using DividerItemDecoration added from support library v25.0.0.

android recyclerview with default divider

2. RecyclerView Divider with custom drawable

Create drawable inside res=>drawable folder

Add below code to your activity

android recyclerview with custom divider

Extending RecyclerView.ItemDecoration


The problem with DividerItemDecoration available is, it also add a divider to our list last row. If you watch carefully the bottom of the list shown above, you find a divider at the end of last item. So to remove it need to extend RecyclerView.ItemDecoration.

1. MyDividerItemDecoration.java

Extending RecyclerView.ItemDecoration class to removing divider from the bottom of last row.

Add below code to your activity

To learn how to implement OnItemClickListener on RecyclerView click here

About Jitendra Singh

Hi there, I'm a founder at androidfizz and a career oriented entity in an area of development of android based mobile applications.

View all posts by Jitendra Singh →

Leave a Reply

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