RecyclerView – Introduction

In this tutorial we are going to learn how to use RecyclerView in our app.

In the Introduction chapter you can read a bit about RecyclerView and you can check the app, what we are going to build up in the tutorial.

Summary in few words

        • Data class
        • Adapter
        • ViewHolder
        • Navigation Component
        • ViewModel + LiveData + MutableLiveData
        • Add, modify and delete item
        • Gestures
          • Reorder
          • Swipe
        • Search and filter items
        • Expandable Floating Action Buttons

GitHub

We have created for every chapters a branch, where you can find the final source code for them. You can open our GitHub page using the above link:

GitHub – Inspire Coding

The app

The chapters

        1. Introduction
          1. History
          2. Why to use
        2. Create a data class
          1. Create the ToDo data class
        3. Add fragments
          1. Create the user interfaces
        4. Navigation component
          1. Add the dependency
          2. Navigation graph
          3. Arguments
          4. Actions
        5. Adapter + ViewHolder
          1. Creating the Adapter
          2. Creating the ViewHolder
          3. ViewModel + LiveData
        6. Gestures
          1. Swipe left and right
          2. Reorder
        7. Delete item
        8. Modify item
        9. Search + filter

Prerequisite

This tutorial assumes, that you have already a basic knowledge about Android development. For this tutorial you should be familiar with the Kotlin programming language. The second thing is that, we will use for the fragment navigation the Jetpack Navigation Component library. If you still don’t know how to use it, then it is highly recommended to do our beginner tutorial.

Navigaion Component

The RecyclerView

The RecyclerView widget is a more advanced and flexible version of ListView.

In the RecyclerView model, several different components work together to display our data. The overall container for our user interface is a RecyclerView object that you add to our layout. The RecyclerView fills itself with views provided by a layout manager that we should provide. We can use one of the standard layout managers (such as LinearLayoutManager or GridLayoutManager), or implement own own manager.

The views in the list are represented by ViewHolder objects. These objects are instances of a class we define by extending RecyclerView.ViewHolder. Each view holder is in charge of displaying a single item with a view. For instance, if our list shows todos collection, each view holder might represent a single task. The RecyclerView creates only as many view holders as are needed to display the on-screen portion of the dynamic content, plus a few extra. As the user scrolls through the list, the RecyclerView takes the off-screen views and rebinds them to the data which is scrolling onto the screen.

The view holder objects are managed by an adapter, which we create by extending RecyclerView.Adapter. The adapter creates view holders as needed. The adapter also binds the view holders to their data. It does this by assigning the view holder to a position, and calling the adapter’s onBindViewHolder() method. That method uses the view holder’s position to determine what the contents should be, based on its list position.

This RecyclerView model does a lot of optimization work. So we don’t have to:
      • When the list is first time populated, it creates and binds some view holders on either side of the list. For instance, if the view is displaying list positions 0 through 9, the RecyclerView creates and binds those view holders, and might also create and bind the view holder for position 10. That way, if the user scrolls the list, the next element is ready to display.
      • As the user scrolls the list, the RecyclerView creates new view holders as necessary. It also saves the view holders which have scrolled off-screen, so they can be reused. If the user switches the direction they were scrolling, the view holders which were scrolled off the screen can be brought right back. On the other hand, if the user keeps scrolling in the same direction, the view holders which have been off-screen the longest can be re-bound to new data. The view holder does not need to be created or have its view inflated; instead, the app just updates the view’s contents to match the new item it was bound to.
      • When the displayed items change, you can notify the adapter by calling an appropriate RecyclerView.Adapter.notify…() method. The adapter’s built-in code then rebinds just the affected items.

 

Questions

I hope the description of the tutorial was understandable and clear. But if you have still questions, then leave me comments below! 😉

Let’s start coding. See you in the next chapter. 😊

 


 

Follow and like us:
Click to rate this post!
[Total: 0 Average: 0]

Leave a Reply

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