Skip to content

Instantly share code, notes, and snippets.

@ravisorathiya
Created September 24, 2022 03:58
Show Gist options
  • Save ravisorathiya/a6a1107eb56ed722782ed4d306f2583c to your computer and use it in GitHub Desktop.
Save ravisorathiya/a6a1107eb56ed722782ed4d306f2583c to your computer and use it in GitHub Desktop.
import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
class MyAdapter(
) : ListAdapter<Any, MyAdapter.MyViewHolder>(
DiffCallBack()
) {
override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int,
): MyViewHolder {
return MyViewHolder(
MyViewHolderBinding.inflate(
LayoutInflater.from(
parent.context
), parent, false
)
)
}
override fun onBindViewHolder(holderPaging: MyViewHolder, position: Int) {
val currentItem = getItem(position)
if (currentItem != null)
holderPaging.bind(currentItem)
}
inner class MyViewHolder(private val itemBinding: MyViewHolderBinding) :
RecyclerView.ViewHolder(itemBinding.root) {
init {
itemBinding.root.setOnClickListener {
val position = bindingAdapterPosition
if (position != RecyclerView.NO_POSITION) {
val item = getItem(position)
onChooseFilter(item)
}
}
}
@SuppressLint("SetTextI18n")
fun bind(item: Any) {
itemBinding.apply {
}
}
}
class DiffCallBack : DiffUtil.ItemCallback<Any>() {
override fun areItemsTheSame(
oldItem: Any,
newItem: Any,
): Boolean =
oldItem.hashCode() == newItem.hashCode()
@SuppressLint("DiffUtilEquals")
override fun areContentsTheSame(
oldItem: Any,
newItem: Any,
): Boolean =
oldItem == newItem
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment