Created
April 26, 2020 06:38
-
-
Save Fbada006/5f3f04b81a47d0816fcd923732f2c69a to your computer and use it in GitHub Desktop.
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
class PojoAdapter : | |
androidx.recyclerview.widget.ListAdapter<Pojo, PojoAdapter.POJOViewHolder>(POJODiffCallback()) { | |
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): POJOViewHolder { | |
return POJOViewHolder.from(parent) | |
} | |
override fun onBindViewHolder(holder: POJOViewHolder, position: Int) { | |
val pojoItem = getItem(position) | |
holder.bind(pojoItem) | |
} | |
class POJOViewHolder constructor(private val binding: PojoItemBinding) : | |
RecyclerView.ViewHolder(binding.root) { | |
fun bind(item: Pojo) { | |
binding.pojo = item | |
binding.executePendingBindings() | |
} | |
//For inflating the layout in onCreateViewHolder() | |
companion object { | |
fun from(parent: ViewGroup): POJOViewHolder { | |
val binding = | |
PojoItemBinding.inflate(LayoutInflater.from(parent.context), parent, false) | |
return POJOViewHolder(binding) | |
} | |
} | |
} | |
} | |
class POJODiffCallback : DiffUtil.ItemCallback<Pojo>() { | |
override fun areItemsTheSame(oldItem: Pojo, newItem: Pojo): Boolean { | |
return oldItem.id == newItem.id | |
} | |
override fun areContentsTheSame(oldItem: Pojo, newItem: Pojo): Boolean { | |
return oldItem == newItem | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment