Writing RecyclerView adapter in Kotlin (KAD 16)

Posted by flamtech on Wed, 17 Jul 2019 23:39:11 +0200

Authors: Antonio Leiva

Time: Mar 14, 2017

Link to the original: https://antonioleiva.com/recyclerview-adapter-kotlin/

 

By creating a RecyclerView adapter, Kotlin can simplify your life, which is an interesting approach.

 

In this approach, you can see more readable code organization and avoid redundant code.

 

Kotlin's Recycler View adapter

 

When we create an adapter, we set the title and insert the graphics into the cells.

 

We don't allow project changes. This is a very simple adapter. If we want to update the data, we need to create a new adapter and set the data to RecyclerView.

 

Model

 

 

We also use a very simple model, which requires only the URL s of identifiers, titles and graphics.

 

We'll use a data class, you remember us I've seen it in previous articles.:

1 data class Item(val id: Long, val title: String, val url: String)

 

So we already have a class and its constructors, immutable properties, and some useful function implementations, such as equals or hashCode.

 

Adapter

 

The structure of the adapter is as follows. It creates some necessary methods by itself:

 1 class MyAdapter : RecyclerView.Adapter() {
 2      
 3     override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ViewHolder {
 4     }
 5  
 6     override fun onBindViewHolder(holder: ViewHolder?, position: Int) {
 7     }
 8  
 9     override fun getItemCount(): Int {
10     }
11  
12     class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)
13 }

 

You will see that I have created a ViewHolder class that extends from the original ViewHolder.

 

This is because the adapter requires the implementation of the original abstract class.

 

In addition, some elements are labeled nullable. This is because if the library does not have proper @Nullable and @NonNull annotations, Kotlin has no way to know whether null is allowed, so let's decide.

 

If we create a method by default, it will assume that its value is nullable.

 

However, if we look further at the support library, we will know which values are null, so we can delete it.

 1 class MyAdapter : RecyclerView.Adapter() {
 2     
 3     override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
 4     }
 5  
 6     override fun onBindViewHolder(holder: ViewHolder, position: Int) {
 7     }
 8  
 9     override fun getItemCount(): Int {
10     }
11  
12     class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)
13 }

 

Constructor

 

The adapter needs to receive parameter items and listeners. It's like this:

class MyAdapter(val items: List, val listener: (Item) -> Unit)

 

The implementation of the method is very easy. I use it. Extended Function Method Created in the previous article Expansion view:

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = ViewHolder(parent.inflate(R.layout.view_item))
 
override fun onBindViewHolder(holder: ViewHolder, position: Int) = holder.bind(items[position], listener)
 
override fun getItemCount() = items.size

 

There are three ways to achieve the previous results in a simplified form. In three lines, we implemented the complete adapter.

 

Now go ahead and implement ViewHolder.

 

ViewHolder

 

ViewHolder assigns values from models to their corresponding views:

1 class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
2     fun bind(item: Item, listener: (Item) -> Unit) = with(itemView) {
3         itemTitle.text = item.title
4         itemImage.loadUrl(item.url)
5         setOnClickListener { listener(item) }
6     }
7 }

 

Everything here has been read in other articles: with function ImageView LourUrl Extension FunctionExtending access views with Kotlin Android And Click on the listener mapping.

 

Adapter assignment

 

Now there's only one thing left: assign the adapter to the view:

1 recycler.layoutManager = GridLayoutManager(this, 2)
2 recycler.adapter = MyAdapter(items) {
3     toast("${it.title} Clicked")
4 }

 

The last function is the listener, which receives an item. When you click on this item, the code simply prints the title to it.

 

conclusion

 

Implementing RecyclerView in Kotlin is as simple as that.

 

So far, we have simplified the code to a minimum using some of the tools we have learned.

 

If you want to learn more about all this and create your own Android application in a skillful way, I recommend you get it. Free Guide It teaches you how to build your first project, or Get the book It teaches you how to create a complete application from scratch.

Topics: Android