Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 안드로이드 스튜디오
- Kotlin
- dart
- Github
- studio
- error
- 코루틴
- coroutine
- RecyclerView
- ADB
- WebView
- 스튜디오
- Retrofit
- MVVM
- 레트로핏
- 의존성주입
- 안드로이드스튜디오
- 안드로이드
- 유튜브
- Gradle
- viewpager
- Android
- 코틀린
- 웹뷰
- build
- image
- 안스
- GIT
- 에러
- 깃헙
Archives
- Today
- Total
코딩하는 일용직 노동자
리사이클러뷰 어댑터 보일러플레이트 코드 본문
RecyclerView Adapter Boilerplate Code
#1 기본적인 어댑터
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ViewHolder
class MyAdapter: RecyclerView.Adapter<MyAdapter.MyViewHolder>() {
private var context: Context? = null
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_list, parent, false)
context = parent.context
return MyViewHolder(view)
}
override fun getItemCount(): Int {
TODO("Not yet implemented")
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
TODO("Not yet implemented")
}
class MyViewHolder(itemView: View) : ViewHolder(itemView) {
}
}
#2 데이터를 추가한 어댑터
class UserData(var name: String, var age: Int, var gender: String) {
fun getAgeString(): String = age.toString()
}
import android.view.ViewGroup
import android.view.LayoutInflater
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.synthetic.main.item_list.view.*
class MyAdapter: RecyclerView.Adapter<MyAdapter.MyViewHolder>() {
private val items = mutableListOf<UserData>()
fun setItems(_items: List<UserData>) {
_items.let {
items.clear()
items.addAll(it)
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = MyViewHolder(parent)
override fun getItemCount(): Int = items.size
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
holder.onBindViewHolder(items[position])
}
inner class MyViewHolder(parent: ViewGroup) : RecyclerView.ViewHolder(
LayoutInflater.from(parent.context).inflate(R.layout.item_list, parent, false)
) {
fun onBindViewHolder(item: UserData) {
with(itemView) {
tvName.text = item.name
tvAge.text = item.age.toString()
tvGender.text = item.gender
}
}
}
}
<?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">
<LinearLayout
android:layout_width="0dp"
android:layout_height="60dp"
android:orientation="horizontal"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent">
<TextView
android:id="@+id/tvName"
android:layout_width="100dp"
android:layout_height="match_parent"/>
<TextView
android:id="@+id/tvAge"
android:layout_width="100dp"
android:layout_height="match_parent"/>
<TextView
android:id="@+id/tvGender"
android:layout_width="100dp"
android:layout_height="match_parent"/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
#3 데이터 바인딩을 이용한 어댑터
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.lee.storagetest.databinding.ItemListBinding
class MyAdapter: RecyclerView.Adapter<MyAdapter.MyViewHolder>() {
private val items = mutableListOf<UserData>()
fun setItems(_items: List<UserData>) {
_items.let {
items.clear()
items.addAll(it)
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
var binding = ItemListBinding.inflate(LayoutInflater.from(parent.context), parent, false)
return MyViewHolder(binding, binding.root)
}
override fun getItemCount(): Int = items.size
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
holder.bind(items[position])
}
inner class MyViewHolder(private val binding: ItemListBinding, itemView: View) : RecyclerView.ViewHolder(
itemView
) {
fun bind(item: UserData) {
binding.data = item
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="data"
type="com.lee.storagetest.list.UserData" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="0dp"
android:layout_height="60dp"
android:orientation="horizontal"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent">
<TextView
android:id="@+id/tvName"
android:layout_width="100dp"
android:layout_height="match_parent"
android:text="@{data.name}"/>
<TextView
android:id="@+id/tvAge"
android:layout_width="100dp"
android:layout_height="match_parent"
android:text="@{data.getAgeString()}"/>
<TextView
android:id="@+id/tvGender"
android:layout_width="100dp"
android:layout_height="match_parent"
android:text="@{data.gender}"/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
'안드로이드' 카테고리의 다른 글
코틀린 Singleton Pattern (2) | 2020.06.08 |
---|---|
AES256 암호화/복호화 (0) | 2020.06.08 |
Live Layout Inspector 기능 살펴보기 (0) | 2020.06.04 |
http CLEARTEXT 오류 수정하기. (0) | 2020.06.03 |
안드로이드 스튜디오 4.0 데이타 바인딩 설정법 변경 (0) | 2020.06.01 |