'how to call supportFragmentManager for dialogFragment in onBindViewHolder?

i have a problem here, idk how to show dialogFragment in my Adapter. i want to everytime my itemView click it will show my dialogFragment

this is my onBindViewHolder

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
    val type = list[position]
    holder.binding.tvTitle.text = type.title
    holder.binding.tvIsiNotes.text = type.content

    holder.itemView.setOnClickListener {
        val dialogFragment = AddFragment()
        dialogFragment.show(supportFragmentManager, null)
    }


    if(position == list.size - 1){
        onLoad?.let {
            it()
        }
    }
}

this is my code using navigationcomponent, but i want to show the Dialog.

holder.itemView.setOnClickListener {
        val action = HomeFragmentDirections.actionHomeFragmentToEditFragment(type)
        holder.itemView.findNavController().navigate(action)
    }


Solution 1:[1]

You could define a custom click listener class to pass as a parameter of your adapter:

class YourAdapter(
    private val clickListener: (type: Int) -> Unit
) { 
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val type = list[position]
        ...
        holder.itemView.setOnClickListener(clickListener(type))
        ...
    }
}

Then, instantiate your adapter as:

val adapter = YourAdapter { it ->
    findNavController().navigate(HomeFragmentDirections.actionHomeFragmentToEditFragment(it))
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1