Skip to content

Instantly share code, notes, and snippets.

@aykuttasil
Created April 4, 2018 23:35
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aykuttasil/889f87755b5618dccff2fbd36cd731b6 to your computer and use it in GitHub Desktop.
Save aykuttasil/889f87755b5618dccff2fbd36cd731b6 to your computer and use it in GitHub Desktop.
ListAdapter with DiffUtil ItemViewType configuration
class MyCagriPoolRecyclerViewAdapter : ListAdapter<DummyItem, RecyclerView.ViewHolder>(MyCagriPoolRecyclerViewAdapter.DIFF_CALLBACK()) {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
val view: View
return when (viewType) {
1 -> {
view = LayoutInflater.from(parent.context).inflate(R.layout.item_cagripool, parent, false)
ViewHolder1(view)
}
else -> {
view = LayoutInflater.from(parent.context).inflate(R.layout.customview_process_odemeyap, parent, false)
ViewHolder2(view)
}
}
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
when (holder.itemViewType) {
1 -> (holder as ViewHolder1).bind(getItem(position))
else -> (holder as ViewHolder2).bind(getItem(position))
}
}
override fun getItemViewType(position: Int): Int {
return when (getItem(position).id) {
"1" -> 1
else -> 2
}
}
inner class ViewHolder1(private val mView: View) : RecyclerView.ViewHolder(mView) {
fun bind(item: DummyItem) {
mView.setBackgroundColor(Color.BLUE)
mView.txtAdresFrom.text = item.content
mView.txtAdresTo.text = item.details
mView.txtPrice.text = "150 TL"
}
}
inner class ViewHolder2(private val mView: View) : RecyclerView.ViewHolder(mView) {
fun bind(item: DummyItem) {
mView.EditText_TCKN.setText("Selam Naber")
}
}
companion object {
@JvmStatic
fun DIFF_CALLBACK(): DiffUtil.ItemCallback<DummyItem> = object : DiffUtil.ItemCallback<DummyItem>() {
override fun areItemsTheSame(oldItem: DummyItem?, newItem: DummyItem?): Boolean {
return oldItem?.id == newItem?.id
}
override fun areContentsTheSame(oldItem: DummyItem?, newItem: DummyItem?): Boolean {
return (oldItem?.content == newItem?.content) && oldItem?.details == newItem?.details
}
}
}
}
@dr-lobo
Copy link

dr-lobo commented Nov 11, 2022

non of this code will work

 mView.setBackgroundColor(Color.BLUE)
        
        mView.txtAdresFrom.text = item.content
        mView.txtAdresTo.text = item.details
        mView.txtPrice.text = "150 TL"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment