Android – recyclerView Adapter PART 2

You can read Part 1 here.

Next, step of the recyclerView implementation is to implement the recyclerView Adapter. It has two main jobs:

  1. to create the item views and return them to the recyclerView
  2. and to populate the item views.

RecyclerView creates the items that are visible but also for some before and after. Instead of creating new items, it takes one item that is last/old, moves it to the front and then recyclerView populates it with new data. In other words, the adapter recycles the views/items

Create a recyclerView Adapter

Create a new class file for the adapter.

how to implement recyclerview adapter, <span>Android – recyclerView Adapter PART 2</span>, KandZ

Create an inner class ViewHolder that extends RecyclerView.ViewHolder. ViewHolder holds a reference to the main view and to the contained views. It must have fields for all the views used to display the information we need. Create also a default constructor.

how to implement recyclerview adapter, <span>Android – recyclerView Adapter PART 2</span>, KandZ

Now extend the adapter class, the outer class. It should extend RecyclerView.Adapter that takes a type argument the ViewHolder you created.

how to implement recyclerview adapter, <span>Android – recyclerView Adapter PART 2</span>, KandZ

Now, override the three methods that are important.

how to implement recyclerview adapter, <span>Android – recyclerView Adapter PART 2</span>, KandZ

Before we implement the methods, we need three private fields. Context, LayoutInflater and a List for the data

how to implement recyclerview adapter, <span>Android – recyclerView Adapter PART 2</span>, KandZ

Now while on the Context or List<>, press Alt + Enter and click Add constructor parameters. Select Context, List and click OK

how to implement recyclerview adapter, <span>Android – recyclerView Adapter PART 2</span>, KandZ

It creates a new constructor with those two parameters

how to implement recyclerview adapter, <span>Android – recyclerView Adapter PART 2</span>, KandZ

Now create a LayoutInflater from the context

how to implement recyclerview adapter, <span>Android – recyclerView Adapter PART 2</span>, KandZ

Create a View and return the ViewHolder. Inflate takes 3 parameters, the item layout, the viewgroup that passed, and false because we do not want the view to be attached to its parent.

how to implement recyclerview adapter, <span>Android – recyclerView Adapter PART 2</span>, KandZ

getIemCount must return the total amount of the data we have

how to implement recyclerview adapter, <span>Android – recyclerView Adapter PART 2</span>, KandZ

Before we bind the data to our view we need to get the references for all the views in the item layout. Create public fields so they can be called from the outer class and then get the references in the ViewHolder inner class.

how to implement recyclerview adapter, <span>Android – recyclerView Adapter PART 2</span>, KandZ

Now, it is time to bind the data to the view

how to implement recyclerview adapter, <span>Android – recyclerView Adapter PART 2</span>, KandZ

Now at the activity java class that has the recyclerView, we need to create a reference to the recyclerView

how to implement recyclerview adapter, <span>Android – recyclerView Adapter PART 2</span>, KandZ

Create a new Linear Layout Manager and set it to the recyclerView

how to implement recyclerview adapter, <span>Android – recyclerView Adapter PART 2</span>, KandZ

Associate the RecyclerView adapter with the recyclerView. But first we need to instantiate the adapter with the data.

how to implement recyclerview adapter, <span>Android – recyclerView Adapter PART 2</span>, KandZ

Here is how it looks 😛

how to implement recyclerview adapter, <span>Android – recyclerView Adapter PART 2</span>, KandZ

END OF PART 2

96580cookie-checkAndroid – recyclerView Adapter PART 2Android – recyclerView Adapter PART 2

Related posts