Search code examples
androidkotlinandroid-dialogfragmentandroid-dialogonitemclicklistener

How to access Dialog's view without creating additional class


Does anyone know which context (if that) would I need to use for a click listener in a RecyclerView adapter without having to create a new DialogFragment class? I will need to access and use the dialog's view in future. The previously used code is what I've used in the past for my RecyclerView adapter but it's now redundant when needing to use the custom view.

Unresolved reference: 'layoutInflater'

previously used code

    holder.myButton.setOnClickListener {
        val builder = android.app.AlertDialog.Builder(holder.itemView.context)
        builder.setIconAttribute(R.attr.ivInfo)
        builder.setTitle(R.string.dialog_title)
        builder.setMessage(R.string.dialog_message)
        builder.setView(R.layout.custom_view)
        builder.setPositiveButton(android.R.string.ok){ dialog, _ -> dialog.dismiss() }

        val dialog: android.app.AlertDialog = builder.create()
        dialog.show()
    }

current code

class MyRVAdapter() : RecyclerView.Adapter<MyRVAdapter.ViewHolder>() {
    override fun getItemCount(): Int {
        return myList.size
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.myButton.setOnClickListener {
            customView = holder.itemView.context.this.layoutInflater.inflate(R.layout.fragment_dialog, null)

                val builder = AlertDialog.Builder(holder.itemView.context)
                 builder.setIconAttribute(R.attr.imgNight)
                 builder.setTitle(R.string.dialog_title)
                 builder.setView(customView)
                 builder.setPositiveButton(android.R.string.ok){ dialog, _ -> dialog.dismiss() }
                 builder.show()

                tabLayout = customView.findViewById(R.id.mTabLayout)
                viewPager = customView.findViewById(R.id.mViewPager)

                val adapter = TabbedDialogAdapter(childFragmentManager)
                adapter.addFragment("Tab A", TabbedDialogFragment.createInstance("Description A"))
                adapter.addFragment("Tab B", TabbedDialogFragment.createInstance("Description B"))
                adapter.addFragment("Tab C", TabbedDialogFragment.createInstance("Description C"))

                customView.mViewPager.adapter = adapter
                customView.mTabLayout.setupWithViewPager(customView.mViewPager)
            }
        }
     }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val v = LayoutInflater.from(parent.context).inflate(R.layout.my_cv, parent, false)
        return ViewHolder(v)
    }

    class ViewHolder (itemView : View):RecyclerView.ViewHolder(itemView) {
        val myButton = itemView.findViewById<Button>(R.id.btn_A)!!
    }
}

Solution

  • To solve your problem you can use View.inflate().

    Change this line:

    customView = holder.itemView.context.this.layoutInflater.inflate(R.layout.fragment_dialog, null)

    to this:

    customView = View.inflate(holder.itemView.context, R.layout.fragment_dialog, null)

    But you should set listeners on creation of ViewHolder instead. In the constructor of ViewHolder or onCreateViewHolder().