I am trying to add View Binding in my RecyclerView's adapter like this:-
class MyAdapter(private val myList: List<MyData>) : RecyclerView.Adapter<MyViewHolder>()
{
private var _binding: MyItemBinding? = null
private val binding get() = _binding!!
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder
{
_binding = MyItemBinding.inflate(LayoutInflater.from(parent.context))
return MyViewHolder(
binding.root)
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int)
{
val itemData = myList[position]
binding.myItemImage.setImageResource(itemData.myImage)
}
override fun getItemCount(): Int = myList.size
}
class MyViewHolder(internal var containerView: View) : RecyclerView.ViewHolder(containerView)
To avoid any memory leaks, we should call destory the binding by setting
_binding = null
But I am not able to find the callback corresponding to onCreateViewHolder()
where I can do that. There is no callback like onDestroyViewHolder()
in the lifecycle of RecyclerView.Adapter
.
I need help in understanding where should I destroy the binding?