-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathActorAdapter.kt
48 lines (35 loc) · 1.36 KB
/
ActorAdapter.kt
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package com.moczul.diffutilsample
import android.support.v7.util.DiffUtil
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
class ActorAdapter(initActorsList: List<Actor>) : RecyclerView.Adapter<ActorAdapter.ViewHolder>() {
private val actors = mutableListOf<Actor>()
init {
actors.addAll(initActorsList)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val inflater = LayoutInflater.from(parent.context)
val view = inflater.inflate(R.layout.item_actor, parent, false)
return ViewHolder(view)
}
override fun getItemCount() = actors.size
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.bind(actor = actors[position])
}
fun swap(actors: List<Actor>) {
val diffCallback = ActorDiffCallback(this.actors, actors)
val diffResult = DiffUtil.calculateDiff(diffCallback)
this.actors.clear()
this.actors.addAll(actors)
diffResult.dispatchUpdatesTo(this)
}
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
private val name: TextView = itemView.findViewById(R.id.actor_name)
fun bind(actor: Actor) {
name.text = actor.name
}
}
}