What is the purpose of the RecyclerView.Adapter class when using RecyclerView?

5

To use RecylerView from Android we need to create a CustomAdapter class that extends from abstract class RecyclerView.Adapter , then implement three methods that are:

  • onCreateViewHolder
  • onBindViewHolder
  • getItemCount
The getItemCount method knows that it returns the number of items in RecyclerView, but the Adapter class itself and the onCreateViewHolder and onBindViewHolder methods leaves me confused and in doubt as to its purposes and functions, these questions will be addressed below.

Questions

  • What is the purpose of class CustomAdapter when we use the RecyclerView?
  • What is the purpose of the onCreateViewHolder method?
  • What is the purpose of the onBindViewHolder method?
  • asked by anonymous 18.09.2017 / 00:00

    2 answers

    5
      

    What is the purpose of the CustomAdapter class when we are going to use the   RecyclerView?

    Initially Adapter , or adapter, let's put it this way, is responsible for making a display for each item in a dataset. That is, it represents the link between the View and some data source, which usually come in two types: represented by data based on array's or lists; or representations based on cursor data.

    With this in mind, the name CustomAdapter already expresses its meaning, making explicit that it is a type of Adapter , however customized, that is, a custom adapter. There is the SimpleAdapter , a simple adapter used to customize list or grid items. This adapter acts as a bridge between a AdapterView and the data intended for that View . CustomAdapter is a little further, as it gives you more authority over views , so you can make any kind of modification to the item.

      

    What is the purpose of the onCreateViewHolder method?

    The onCreateViewHolder() serves to inflate the layout of the item. It's basically called when you need to create a new item.

      

    What is the purpose of the onBindViewHolder method?

    The method onBindViewHolder() " is intended to set the display attributes based on the data. It is basically invoked when an item needs to be displayed to the user.

        
    18.09.2017 / 05:32
    2
      

    What is the purpose of the CustomAdapter class when we are going to use RecyclerView?

    A RecyclerView should be able to visually display a variety of different data types and shapes. For this to be easily implemented, RecyclerView uses an object of type RecyclerView.Adapter . It's your job to pick up the data from any data source and "transform" them into views to be used by RecyclerView.

      

    What is the purpose of the onCreateViewHolder method?   What is the purpose of the onBindViewHolder method?

    These two methods are the "heart" of the adapter, its implementation serving the purpose of the adapter.

    The onCreateViewHolder() method is called by RecyclerView when it needs a new view. The onBindViewHolder() method is called by RecyclerView when it needs new data to be assigned to a view.

    Both use a RecyclerView.ViewHolder object "where they are saved "the views. In the onCreateViewHolder() method the ViewHolder is created and then passed to the onBindViewHolder() method when it is necessary to assign data to the views. Using ViewHolder in conjunction with both methods allows unused views to be reused when needed.

        
    18.09.2017 / 12:59