To create a recycler view in kotlin you need three classes, an Adapter class to set data to items in the recycler view a data class to give your data a structure, and an activity or fragment class to display the view.
Add dependencies
- Add the following dependencies to your build.gradle file
1
2
| implementation 'androidx.recyclerview:recyclerview:1.1.0'
implementation 'androidx.cardview:cardview:1.0.0'
|
Write Code
- Create a data class, you can pass the variables you want to access later as parameters in the data class
1
| data class Item(val text: String)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
| class Adapter(private val List: List<Item>) : RecyclerView.Adapter<Adapter.ViewHolder>() {
class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val text: TextView = view.findViewById(R.id.text)
init {
// You can add onClick Listeners here
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val item = LayoutInflater.from(parent.context).inflate(R.layout.item, parent, false)
return ViewHolder(item)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val element = List[position]
holder.text.text = element.text
}
override fun getItemCount(): Int {
return List.size
}
}
|
- Create an Activity where you want to display here I am feeding the view with dummy data you can add whatever data you want
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
view.adapter = Adapter(generateDummyList(1000))
view.layoutManager = LinearLayoutManager(this)
view.hasFixedSize()
}
private fun generateDummyList(size: Int): List<Item> {
val list = ArrayList<Item>()
for (i in 0 until size) {
val item = Item("Item $i")
list += item
}
return list
}
}
|
The entire code used is in the GitHub repo — https://github.com/vineelsai26/Recycler-View-Kotlin
Happy Coding!!!