Recyclerview in KOTLIN
Hello Readers,
Recently I have started learning KOTLIN and I am in love with it. It seems good language and future will be bright hopefully. If you want to learn all the basic please follow https://kotlinlang.org/docs/reference/ .
Now lets talk about KOTLIN in android as its offical language for android.
Before starting you need :
1- Android studio 3.o Canary https://developer.android.com/studio/preview/index.html
2- Basic knowledge of Kotlin https://kotlinlang.org/docs/reference/
So lets get started.
1- Open Android stuido 3.0
2- Go to File -> New Project
3- Write your app name in window and check the support for Kotlin
Now you can see project has been created so lets start coding.
activity_main.xml
Write a layout which is having recycler view in it.
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_name"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
Write a layout which is having textview in it. As we are going to show only list of names in it.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tv_name"
android:padding="10dp"
android:textStyle="bold"
android:textSize="20sp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Create a kotlin file named as you want to give, for me its NameAdapter.kt
class NameAdapter(val nameList : ArrayList<String>, val listener : ItemListener) :
RecyclerView.Adapter<NameAdapter.NameViewHolder>() {
/**
* Creator function
*/
override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): NameViewHolder
{
val view = LayoutInflater.from(parent?.getContext())
.inflate(R.layout.layout_item, parent, false);
//return ViewHolder
return NameViewHolder(view)
}
/**
* Binder function
*/
override fun onBindViewHolder(holder: NameViewHolder, position: Int)
{
holder.bindData(nameList.get(position), listener)
}
/**
* Returns item counts
* or list size
*/
override fun getItemCount(): Int
{
return nameList.size
}
class NameViewHolder(itemView: View?) : RecyclerView.ViewHolder(itemView)
{
var tvName = itemView?.findViewById<TextView>(R.id.tv_name) fun bindData(name : String , listener: ItemListener) {
tvName?.text = name
tvName?.setOnClickListener { itemView ->
listener.onClicked(name)
}
}
}
}
Create one file for Interface, for me its ItemListener.kt and provide some function in it which will get call on click of items in list.
interface ItemListener {
fun onClicked(name :String)
}
Calling for interface function happened in NameAdapter.kt file
tvName?.setOnClickListener { itemView ->
listener.onClicked(name)
}
Now its time to finalize our work and see the output. Lets see Activity code
As we do not have any implements or extend keyword in Kotlin so you can implement or extends by : colon and separated by , comma if more than one implementation is there
class MainActivity : AppCompatActivity(), ItemListener {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(toolbar)
rv_name.layoutManager = LinearLayoutManager(this)
rv_name.adapter = NameAdapter(getNameList(), this)
}
/**
* Function return list of names
*/
fun getNameList() : ArrayList<String>
{
val list = ArrayList<String>()
list.add("Alex")
list.add("Andy")
list.add("Himanshu")
list.add("Ronny")
list.add("Abhi")
list.add("Rojer")
list.add("Shiva")
return list
}
/**
* Function called when any item clicked
*/
override fun onClicked(name : String)
{
Toast.makeText(this, "Name is "+name, Toast.LENGTH_SHORT).show()
}
}
Will be sharing full code on github soon.
Happy Coding cheers!!!!