Created
September 22, 2022 14:28
-
-
Save ercnksgl/dbc54b94e1ce45ada6b99645ecf56397 to your computer and use it in GitHub Desktop.
ItemAdapter for DiffUtils test app
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.view.LayoutInflater | |
import android.view.View | |
import android.view.ViewGroup | |
import android.widget.TextView | |
import androidx.recyclerview.widget.DiffUtil | |
import androidx.recyclerview.widget.RecyclerView | |
class ItemAdapter : RecyclerView.Adapter<ItemAdapter.ViewHolder>() { | |
private var list = emptyList<Item>() | |
fun setItems(items: List<Item>) { | |
val diffCallback = CustomDiffUtils(list, items) | |
val diffResult = DiffUtil.calculateDiff(diffCallback) | |
this.list = items | |
diffResult.dispatchUpdatesTo(this) | |
} | |
fun getCurrentList() = list | |
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemAdapter.ViewHolder { | |
val view = LayoutInflater.from(parent.context).inflate(android.R.layout.simple_list_item_1, parent, false) | |
return ViewHolder(view) | |
} | |
override fun onBindViewHolder(holder: ItemAdapter.ViewHolder, position: Int) { | |
holder.bind(list[position]) | |
} | |
override fun getItemCount() = list.size | |
inner class ViewHolder(private val view: View) : RecyclerView.ViewHolder(view) { | |
fun bind(item: Item) { | |
val textView = view.findViewById<TextView>(android.R.id.text1) | |
textView.text = item.name | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment