Write the RecyclerView adapter (KAD 16) and kotlinrecyclerview in Kotlin.

Source: Internet
Author: User

Write the RecyclerView adapter (KAD 16) and kotlinrecyclerview in Kotlin.

By Antonio Leiva

Time: Mar 14,201 7

Link: https://antonioleiva.com/recyclerview-adapter-kotlin/

 

By creating a RecyclerView adapter, Kotlin can simplify your life. This is an interesting method.

 

In this method, you can see Code organizations that are easier to read and avoid redundant code.

 

Kotlin RecyclerView Adapter

 

When creating an adapter, we need to set the title and insert the image into each unit.

 

We do not 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 dataRecyclerView.

 

Model

 

 

We also use a very simple model that requires only one identifier, title, and image URL.

 

We will use a data class. Remember the previous articles we have seen:

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

 

In this way, we already have a class and its constructor, immutable attributes, and some useful function implementations, such:equalsOrhashCode

 

Adapter

 

The structure of the adapter is as follows:

 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 createdViewHolderExtendedViewHolderClass.

 

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

 

In addition, some elements are markedNullable. This is because if the library does not have an appropriate@NullableAnd@NonNullAnnotation, Kotlin has no way to know whether null is allowed, which is determined by us.

 

If we create a method by default, it considers its value to beNullable.

 

However, if we further study 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. This is like this:

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

 

Method implementation is very easy. I used the Extended Function Method expansion view created in the previous article:

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 methods to achieve the previous results in a simple form. With three lines, we have implemented the complete adapter.

 

Implement it nowViewHolder.

 

ViewHolder

 

ViewHolderAssign values from the model 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.title4         itemImage.loadUrl(item.url)5         setOnClickListener { listener(item) }6     }7 }

 

Everything here has been read in other articles: the loadUrl Extension function of the with function and ImageView, using Kotlin Android to expand the access view and click the ing of the listener.

 

Adapter assignment

 

There is 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 one. When you click this item, the Code simply prints the title to the item.

 

Conclusion

 

Implementation in KotlinRecyclerViewThat's simple.

 

So far, we have used some learned tools to simplify the code to a minimum.

 

If you want to learn more about this and create your own Android app in a skillful way, I suggest you get a free guide to teach you how to build your first project, you can also obtain this book to learn how to create a complete application from scratch.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.