How to access custom view inflated in DialogFragment

时间:2019-02-18 00:23:14

标签: android android-fragments kotlin android-dialogfragment

I need to access the view that has been inflated in a DialogFragment. inflater and container appear to be unresolved but yet when I add these to the parentheses of onCreateDialog, the overrides nothing error is returned. What is the correct solution in this case?

class MyDialogFragment : DialogFragment() {
    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val builder = AlertDialog.Builder(activity)
        val rootView = inflater.inflate(R.layout.dialog_sample, container, false)

        lateinit var tabLayout: TabLayout
        lateinit var viewPager: ViewPager

        builder.setIconAttribute(R.attr.imgNight)
        builder.setTitle("My Program")
        builder.setView(rootView)
        builder.setPositiveButton(getString(R.string.ok)) { dialog, _ -> dialog.dismiss() }

        tabLayout = rootView.findViewById(R.id.tabLayout)
        viewPager = rootView.findViewById(R.id.viewPager)

        val adapter = CustomAdapter(childFragmentManager)
        adapter.addFragment("Boy", CustomFragment.createInstance("Oscar"))
        adapter.addFragment("Girl", CustomFragment.createInstance("Stacy"))
        adapter.addFragment("Robot", CustomFragment.createInstance("Aeon"))
        viewPager.adapter = adapter
        tabLayout.setupWithViewPager(viewPager)

        return builder.create()
    }
}

1 个答案:

答案 0 :(得分:0)

The container pass null since there isn't a parent view. As for the inflater use LayoutInflater.from(requireContext).

You can then use the view in the dialog builder.