All you need to know about RecyclerView + Example

Liked this Post?

Share on facebook
Share on Facebook
Share on twitter
Share on Twitter
Share on reddit
Share on Reddit
Share on linkedin
Share on LinkedIn
Share on whatsapp
Share on WhatsApp
Share on telegram
Share on Telegram

Welcome to RecyclerView!

What is a RecyclerView?

RecyclerView is a container for displaying large data sets that can be scrolled (vertical or horizontal) very efficiently by maintaining a limited number of views.

An Adapter is necessary to pull content from a source such as an array or database. An adapter actually bridges between UI components and the data source that fill data into UI Component.

Every piece of recycler view is completely modular to allow for easy customisations such as animations, layout style (linear, grid or staggered layout), listeners and so on.

When to use a RecyclerView

It’s ideal to use to use a RecyclerView when the data to be presented is similar and has unpredictable quantity but also large (depending on the user).

Ideal use cases are Photo Gallery App, Gmail, ToDo List App, Chat Apps etc.

It is impractical and memory consuming to create views for each data because User A might have 10 photos in her Gallery while User B might have 10 thousand. Now, let’s imagine creating 10,000 imageView’s to display User B’s content. Our app would consistently crash due to lack of memory to display all 10 thousand pictures at once. RecyclerView solves this problem by caching and re-using views.

How does RecyclerView work?

Good Question.

When a RecyclerView is successfully initialised with an adapter, the adapter creates a number of views that fills the visible screen plus extra views above and below the screen. For instance, If your screen can accommodate 7 row items, the adapter would create 7+ ViewHolders. This is done in order to handle fast flings and smoothly scroll through a list from an array or data source. ViewHolders are created in onCreateViewHolder. By employing the ViewHolder pattern, RecyclerView adapter can recycle or create new ViewHolders when needed, which is far more efficient than creating new views for each item.

  1. Adapter : The recycler view adapter is used to provide the recycler view with new views when needed and can also be used to bind data from some data source to the views. The adapter sends views to an object called a ViewHolder. In order to create RecyclerView adapter you need to sub-cass RecyclerView.Adapter while passing in a generic subclass of RecyclerView.ViewHolder.
  2. ViewHolder: describes an item view and metadata about its place within the RecyclerView. When recycler view is first being populated you call findViewById for each view that will be showing data from the adaptor, this search can be expensive especially if you have a hierarchy of view in your list item. So it’s best to do it once and cache it in a viewHolder. This way you can access the views later without having to look them up repeatedly

Let’s talk about the Recycler.Adapter class :

  • it mandates the implementation of View Holder pattern (which is fancy word for re-using views to inflate data instead of creating views for every data in the array or data source)
  • it mandates overriding 3 methods – onCreateViewHolder, getItemCount, onBindViewHolder.
  • onCreateViewHolder is called when you need a new View. If there is an available Recycled View that can be provided and be bound with new data, then onBindViewHolder is called.
  • onBindViewHolder is used to update the RecyclerView.ViewHolder contents with the item at the given position and also sets up some private fields to be used by RecyclerView.
  • getItemCount returns the total number of items in the data set held by the adapter.

How to use Recycler View

To use the recycler view, you will need a view holder and an adapter. Adapters are used to link UI elements with code, while view holders contain views.

Basic Tutorial on Recycler View

[button color=”red” size=”medium” link=”” icon=”” target=”true”]Source Code is Available on GitHub[/button]

1. Add Recycler View dependency to your build.grade(Module : App) file

As of this writing, the latest version is implementation ‘

Check for latest support library versions

2. Activity Layout (activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
< xmlns:android=""

app:layout_constraintTop_toTopOf="parent" />


3. Layout that displays data in Rows – recycler_view_row.xml

<?xml version="1.0" encoding="utf-8"?>

tools:text="Title 1" />

tools:text="This is Sub-TITLE 1 with a summary" />


4. MainActivity.kt

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {

        val recyclerView = findViewById<RecyclerView>(
        recyclerView.layoutManager = LinearLayoutManager(this)
        val recyclerViewItemDivider = DividerItemDecoration(this, LinearLayoutManager.VERTICAL)
        val recyclerViewAdapter = RecyclerViewAdapter(generateDummyData())
        recyclerView.adapter = recyclerViewAdapter

    private fun generateDummyData(): List<DummyData> {
        val dummyDataList = ArrayList<DummyData>()
        for (x in 0..50) {
            dummyDataList.add(DummyData("Title $x", "This is Sub-TITLE $x with a summary"))
        return dummyDataList

    class RecyclerViewAdapter(var dummyDataList: List<DummyData>) : RecyclerView.Adapter<ViewHolder>() {
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
            val view = LayoutInflater.from(parent.context).inflate(R.layout.recycler_view_row, parent, false)
            return ViewHolder(view)

        override fun getItemCount() = dummyDataList.size

        override fun onBindViewHolder(holder: ViewHolder, position: Int) {
    class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val titleTextView: TextView
        val subTitleTextView: TextView

        init {
            titleTextView = view.findViewById(
            subTitleTextView = view.findViewById(
            view.setOnClickListener { view ->
                Toast.makeText(view.context,"You clicked on Item ${adapterPosition}", Toast.LENGTH_SHORT).show()


About the Author

Edge Developer

Edge Developer

Hello there, my name is Opeyemi Olorunleke. I am a Software Developer (majorly Android, GitHub Profile), Digital Marketer, Udemy Instructor, Technical Writer, Blogger & Webmaster.

Related Posts

Leave a Comment

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