activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rec"/>
</LinearLayout>
MainActivity.kt
package com.example.recycle
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
class MainActivity : AppCompatActivity() {
var Course = arrayOf( "Android", "2", "PHP", "Android", "Java", "PHP", "Android", "Java", "PHP","Android","Java","PHP",
"Android","Java","PHP","Android","Java","PHP","Android","Java","PHP","Android","Java","PHP","Android","Java","PHP",
"Android","Java","PHP","Android","Java","PHP","Android","Java","PHP","Android","Java","PHP","Android","Java","PHP",
"Android","Java","PHP" )
var recyclerView: RecyclerView? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
recyclerView = findViewById<View>(R.id.rec) as RecyclerView
val layoutManager = LinearLayoutManager(this@MainActivity,LinearLayoutManager.VERTICAL,true)
recyclerView!!.layoutManager = layoutManager
val customAdapter = CustomAdapter(this@MainActivity, Course)
recyclerView!!.adapter = customAdapter
}
}
CustomAdapter.kt
package com.example.recycle
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.RecyclerView
class CustomAdapter(mainActivity: AppCompatActivity, course: Array<String>) :
RecyclerView.Adapter<CustomAdapter.MyViewHolder>() {
var Names: Array<String>
var context: Context
init {
context = mainActivity
Names = course
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
val v =
LayoutInflater.from(parent.context).inflate(R.layout.rowlayout, parent, false)
return MyViewHolder(v)
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
holder.name.text = Names[holder.adapterPosition]
holder.name.setOnClickListener {
Toast.makeText(
context,
Names[holder.adapterPosition],
Toast.LENGTH_SHORT
).show()
}
}
override fun getItemCount(): Int {
return Names.size
}
inner class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
var name // init the item view's
: TextView
init {
// get the reference of item view's
name = itemView.findViewById<View>(R.id.name) as TextView
}
}
}
rowlayout.xml
<?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"
android:orientation="vertical"
android:background="#fff">
<!--
items for a single row of RecyclerView
-->
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:textColor="#FF5722"
android:textSize="20sp" />
</LinearLayout>