ListAdapter에 대해서 간단하게 보고 가겠습니다.
RecyclerView를 사용할 때 데이터를 바꿔줄 때가 있습니다. 그럴 때 마다 notifyDataSetChanged() 를 호출하여 전체 데이터를 고치는데 diffUtil을 이용하여 변경된 데이터만 바꿔주며 아래와 같은 시간절약 효과가 있습니다.
MainActivity.kt
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val recyclerView = findViewById<RecyclerView>(R.id.programmingList)
val adapter = ProgrammingAdapter()
val p1 = ProgrammingItem(1,"J", "java")
val p2 = ProgrammingItem(2,"K", "Kotlin")
val p3 = ProgrammingItem(3,"A", "Android")
adapter.submitList(listOf(p1,p2,p3))
recyclerView.layoutManager = LinearLayoutManager(this)
recyclerView.setHasFixedSize(true)
recyclerView.adapter = adapter
Handler(Looper.getMainLooper()).postDelayed(Runnable {
val p3 = ProgrammingItem(3,"A", "Android")
val p4 = ProgrammingItem(4,"C", "C")
val p5 = ProgrammingItem(5,"V", "V")
val p6 = ProgrammingItem(6,"B", "B")
adapter.submitList(listOf(p3,p4,p5,p6))
}, 4000)
}
}
ProgrammingAdapater.kt
class ProgrammingAdapter : ListAdapter<ProgrammingItem, ProgrammingAdapter.ProgrammingViewHolder>(DiffUtil()) {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ProgrammingViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_view, parent, false)
return ProgrammingViewHolder(view)
}
override fun onBindViewHolder(holder: ProgrammingViewHolder, position: Int) {
val item = getItem(position)
holder.bind(item)
}
class ProgrammingViewHolder(view : View) : RecyclerView.ViewHolder(view) {
val name = view.findViewById<TextView>(R.id.name)
val initial = view.findViewById<TextView>(R.id.initial)
fun bind(item : ProgrammingItem) {
name.text = item.name
initial.text = item.initial
}
}
class DiffUtil : androidx.recyclerview.widget.DiffUtil.ItemCallback<ProgrammingItem>(){
override fun areItemsTheSame(oldItem: ProgrammingItem, newItem: ProgrammingItem): Boolean {
return oldItem.id == newItem.id
}
@SuppressLint("DiffUtilEquals")
override fun areContentsTheSame(
oldItem: ProgrammingItem,
newItem: ProgrammingItem,
): Boolean {
return oldItem == newItem
}
}
}
ProgrammingItem.kt
class ProgrammingItem(val id : Int, val initial : String, val name : String)
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/programmingList"
android:layout_width="match_parent"
android:layout_height="match_parent"
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_view.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">
<TextView
android:id="@+id/initial"
android:layout_width="42dp"
android:layout_height="42dp"
android:text="j"
android:background="@drawable/item_red"/>
<TextView
android:id="@+id/name"
android:text="java"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
item_red.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="150dp"></corners>
<solid android:color="#F44336"></solid>
</shape>
- 참조
https://developer.android.com/reference/androidx/recyclerview/widget/DiffUtil
https://www.youtube.com/watch?v=QsilZEC-cOs&t=552s
'Android(Kotlin)' 카테고리의 다른 글
LiveData emit / emitSource (0) | 2022.12.13 |
---|---|
Application Class (0) | 2022.12.06 |
by viewModels() (0) | 2022.12.03 |
PeriodicWorkManager Unique Work (1) | 2022.11.21 |
Android App Inspector (1) | 2022.11.12 |