Android RecyclerView – Kotlin

RecyclerView

RecyclerView is similar with ListView.
(But not equal)
This is in support v7.
RecyclerView seems to have low level compare to ListView.

Preparation

Add support library to build.gradle

implementation 'com.android.support:recyclerview-v7:26.1.0'

This is an example, please be careful version number, I use 26 compileSdkVersion.

Component(class)

To use this, we need to prepare some classes to build it

Name What?
RecyclerView View widget, it’s same as ListView
RecyclerView.Adapter Data adapter, it is same as Adapter
RecyclerView.ViewHolder 1 line view layout referrence
RecyclerView.LayoutManager

ItemData.kt

This is data

class ItemData(title : String) {

    var title : String

    init {
        this.title = title
    }
}

ItemDataViewHolder.kt

class ItemDataViewHolder(parent : ViewGroup) : RecyclerView.ViewHolder(
        LayoutInflater.from(parent.context).inflate(R.layout.list_row_item, parent, false)) {

    val titleView : TextView = itemView.findViewById(R.id.titleView)
}

list_row_item.xml

XML layout for ItemDataViewHolder,… 1 line layout

ItemDataRecycleViewAdapter.kt

class ItemDataRecycleViewAdapter(list: List<ItemData>) : RecyclerView.Adapter<ItemDataViewHolder>() {

    private val list : List<ItemData>

    init {
        this.list = list
    }

    override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ItemDataViewHolder {
        var holder : ItemDataViewHolder = ItemDataViewHolder(parent!!)
        return holder
    }

    override fun onBindViewHolder(holder: ItemDataViewHolder?, position: Int) {
        holder?.titleView?.text = list.get(position).title
    }

    override fun getItemCount(): Int {
        return list.size
    }
}

MainActivity.kt

class MainActivity : AppCompatActivity() {

    private var adapter : ItemDataRecycleViewAdapter? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        var rv : RecyclerView = findViewById(R.id.recyclerview)
        adapter = ItemDataRecycleViewAdapter(getItems())

        var llm = LinearLayoutManager(this)
        rv.adapter = adapter
        rv.layoutManager = llm
        rv.setOnClickListener(ItemClickListener()) // No item click listener?
    }

    private fun getItems() : List<ItemData> {
        var list : MutableList<ItemData> = mutableListOf()
        list.add(ItemData("Ash"))
        list.add(ItemData("Miona"))
        list.add(ItemData("Maiyan"))
        list.add(ItemData("Naachan"))
        list.add(ItemData("Manatsu"))
        list.add(ItemData("Ikuchan"))
        list.add(ItemData("Ikomachan"))
        return list
    }

    inner class ItemClickListener : View.OnClickListener {
        override fun onClick(p0: View?) {

        }
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.atmarkplant.recyclerviewsample.MainActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerview"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </android.support.v7.widget.RecyclerView>

</android.support.constraint.ConstraintLayout>

Result

Ref