build.gradle(Moudle) viewBinding true 설정
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:id="@+id/parent"
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_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
item_recycler.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="50dp"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:id="@+id/textNo"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="01" />
<TextView
android:id="@+id/textTitle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="5"
android:text="Title" />
<TextView
android:id="@+id/textDate"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="2021-01-01" />
</LinearLayout>
아래와 같이 데이터 클래스를 만들어 줍니다.
Memo.kt
data class Memo(var no: Int, var title: String, var timestamp: Long)
AdapterClass도 만들어 줍니다
CustomAdapter.kt
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.Toast
import androidx.recyclerview.widget.RecyclerView
import org.techtown.location.kotlinproject.databinding.ItemRecyclerBinding
import java.text.SimpleDateFormat
class CustomAdapter: RecyclerView.Adapter<Holder>() {
var listData = mutableListOf<Memo>()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): Holder {
val binding = ItemRecyclerBinding.inflate(LayoutInflater.from(parent.context),
parent, false);
return Holder(binding)
}
override fun getItemCount(): Int {
return listData.size
}
override fun onBindViewHolder(holder: Holder, position: Int) {
val memo = listData.get(position)
holder.setMemo(memo)
}
}
class Holder(val binding: ItemRecyclerBinding): RecyclerView.ViewHolder(binding.root){
init{
binding.root.setOnClickListener{
Toast.makeText(binding.root.context, "클릭된 아이템 = ${binding.textTitle.text}",
Toast.LENGTH_LONG).show()
}
}
fun setMemo(memo: Memo){
binding.textNo.text = "${memo.no}"
binding.textTitle.text = memo.title
var sdf = SimpleDateFormat("yyyy/MM/dd")
binding.textDate.text = sdf.format(memo.timestamp)
}
}
MainActivity.kt
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import org.techtown.location.kotlinproject.databinding.ActivityMainBinding
import kotlin.concurrent.thread
class MainActivity : AppCompatActivity() {
val binding by lazy { ActivityMainBinding.inflate(layoutInflater)}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(binding.root)
var adapter = CustomAdapter()
adapter.listData = loadData()
binding.recyclerView.adapter = adapter
/* recycyclerView Option */
binding.recyclerView.layoutManager = LinearLayoutManager(this)
// binding.recyclerView.layoutManager = LinearLayoutManager(this,
//LinearLayoutManager.HORIZONTAL,false)
// GridLayoutManager(this, 3)
// StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL)
}
fun loadData(): MutableList<Memo>{
val data : MutableList<Memo> = mutableListOf()
for(no in 1..100){
val title = "예제 ${no} 입니다 "
val date = System.currentTimeMillis()
var memo = Memo(no, title, date)
data.add(memo)
}
return data
}
}
[결과]
'Kotlin' 카테고리의 다른 글
Android Kotlin - DIP(Dependency Inversion Principle)이란? (0) | 2023.07.14 |
---|---|
[Kotlin] - 코틀린 기본 문법 4 (0) | 2022.07.10 |
[Kotlin] - 코틀린 기본 문법 3 (0) | 2022.07.09 |
[Kotlin] - 코틀린 기본 문법 2 (0) | 2022.07.08 |
[Kotlin] - 코틀린 기본 문법 1 (0) | 2022.07.08 |