我无法在片段中使用 kotlin 定义 supportFragmentManager

I can't define supportFragmentManager with kotlin in fragment

class DataFragment : Fragment() {

private var _binding: FragmentDataBinding? = null
private val binding get() = _binding!!
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    val adapter = MyViewPagerAdapter(supportFragmentManager)

    adapter.addFragment(DataFragment(), "Günlük")
    adapter.addFragment(Data2Fragment(), "Aylık")
    adapter.addFragment(Data3Fragment(), "Yıllık")

    viewPager.adapter = adapter
    tabs.setupWithViewPager(viewPager)

}

override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    _binding = FragmentDataBinding.inflate(inflater, container, false)
    val view = binding.root
    return view
}

我设计了一个tabLayout 并将其放在名为DataFragment 的片段中。我打开了名为 daily、monthly、yearly 的不同片段,并将其添加到代码中。但是我不能在这里定义 supportFragmentManager 。它给出了一个未解决的参考错误。

class MyViewPagerAdapter(manager: FragmentManager) : FragmentPagerAdapter(manager) {

private val fragmentList: MutableList<Fragment> = ArrayList()
private val titleList: MutableList<String> = ArrayList()

override fun getItem(position: Int): Fragment {
    return fragmentList[position]
}
override fun getCount(): Int {
    return fragmentList.size
}
fun addFragment(fragment: Fragment, title: String) {
    fragmentList.add(fragment)
    titleList.add(title)
}
override fun getPageTitle(position: Int): CharSequence? {
    return titleList[position]
}

下面是我定义的适配器的代码。

requireActivity().supportFragmentManager

您必须从父级 activity 调用它,而不是直接从片段

像下面的代码一样简单:

 activity?.supportFragmentManager?.beginTransaction()