activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
item_recyclerview.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="아이템 제목"
android:textSize="20dp"
app:layout_constraintBottom_toTopOf="@id/subtitle"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/subtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="이것은 본문입니다."
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/title" />
</androidx.constraintlayout.widget.ConstraintLayout>
항목 레이아웃은 간단하게 제목과 본문 textView 2개만 구현.
package com.mj.recyclerview_sample
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
class RecyclerViewAdapter(): RecyclerView.Adapter<RecyclerViewAdapter.RecyclerViewViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerViewViewHolder {
val itemView = LayoutInflater.from(parent.context).inflate(R.layout.item_recyclerview, parent, false)
return RecyclerViewViewHolder(itemView)
}
override fun onBindViewHolder(holder: RecyclerViewViewHolder, position: Int) {
holder.bind(recyclerViewItems[position])
}
override fun getItemCount(): Int {
return recyclerViewItems.size
}
inner class RecyclerViewViewHolder(itemView: View): RecyclerView.ViewHolder(itemView) {
private val title: TextView = itemView.findViewById(R.id.title)
private val subtitle: TextView = itemView.findViewById(R.id.subtitle)
fun bind(recyclerViewItem: RecyclerViewItem) {
title.text = recyclerViewItem.title
subtitle.text = recyclerViewItem.subtitle
}
}
}
어댑터를 만들때는 3가지 메서드를 오버라이드 해야한다. 각각 onCreateViewHolder, onBindViewHolder, getItemCount를 구현해야한다. ViewHolder는 아이템에 대한 뷰와 그와 관련된 메타데이터를 가지고 있는 객체이다.
MainActivity.kt
package com.mj.recyclerview_sample
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//리스트에 사용될 ArrayList를 생성한다.
val recyclerViewItems: ArrayList<RecyclerViewItem> = ArrayList()
recyclerViewItems.add(RecyclerViewItem(1, "title1", "subtitle1"))
recyclerViewItems.add(RecyclerViewItem(2, "title2", "subtitle2"))
recyclerViewItems.add(RecyclerViewItem(3, "title3", "subtitle3"))
recyclerViewItems.add(RecyclerViewItem(4, "title4", "subtitle4"))
recyclerViewItems.add(RecyclerViewItem(5, "title5", "subtitle5"))
//recyclerView 연결
val recyclerView = findViewById<RecyclerView>(R.id.recyclerView)
recyclerView.layoutManager = LinearLayoutManager(this)
val adapter = RecyclerViewAdapter()
recyclerView.adapter = adapter
}
}
//리스트에 사용될 객체
data class RecyclerViewItem(
var id: Int = 0,
var title: String = "",
var subtitle: String = ""
)
어댑터 생성자에 클릭 리스너 람다 함수를 매개 변수로 설정하기
class RecyclerViewAdapter(
private var recyclerViewItems: ArrayList<RecyclerViewItem>,
val itemClick: (RecyclerViewItem) -> Unit
): RecyclerView.Adapter<RecyclerViewAdapter.RecyclerViewViewHolder>()
inner class RecyclerViewViewHolder(itemView: View): RecyclerView.ViewHolder(itemView) {
private val title: TextView = itemView.findViewById(R.id.title)
private val subtitle: TextView = itemView.findViewById(R.id.subtitle)
fun bind(recyclerViewItem: RecyclerViewItem) {
title.text = recyclerViewItem.title
subtitle.text = recyclerViewItem.subtitle
itemView.setOnClickListener { itemClick(recyclerViewItem) }
}
}
val adapter = RecyclerViewAdapter(recyclerViewItems) { recyclerViewItem ->
Toast.makeText(this, "${recyclerViewItem.title} 클릭", Toast.LENGTH_SHORT).show()
}