【问题标题】:Error : None of the following functions can be called with the arguments supplied issue in FragmentStateAdapter in viewpager2错误:无法使用 viewpager2 中 FragmentStateAdapter 中提供的参数调用以下函数
【发布时间】:2020-07-17 06:42:33
【问题描述】:

查看我的代码

我在片段中使用 viewpager 2 来保存子片段。可以有“n”个子片段。它在适配器中显示 lint 错误。

//查看我的片段代码

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        val docs = mutableListOf(1,2,3,4,5,6,7,8,9)

        view_pager.adapter = DocAdapter(childFragmentManager,lifecycle,docs)

    }

//我的适配器代码

    import androidx.fragment.app.Fragment
    import androidx.fragment.app.FragmentManager
    import androidx.lifecycle.Lifecycle
    import androidx.viewpager2.adapter.FragmentStateAdapter
    
    class DocAdapter(fa: FragmentManager, lifecycle: Lifecycle, private val docs : MutableList<Int>) :
FragmentStateAdapter(fa) {
    
        override fun getItemCount(): Int = docs.size
    
        override fun createFragment(position: Int): Fragment = DocUploadFragment().newInstance(docs[position])
    }

lint 错误在适配器类的以下行中

class DocAdapter(fa: FragmentManager, lifecycle: Lifecycle, private val docs : MutableList<Int>) :
FragmentStateAdapter(fa) {

【问题讨论】:

    标签: android kotlin android-viewpager2 child-fragment fragmentstateadapter


    【解决方案1】:

    FragmentStateAdapter 在其构造函数中需要 FragmentManagerLifecycle。将您的 DocAdapter 更改为:

    class DocAdapter(fa: FragmentManager, lifecycle: Lifecycle, private val docs : MutableList<Int>) :
        FragmentStateAdapter(fa, lifecycle) {
        ...
    }
    

    【讨论】:

      猜你喜欢
      • 2023-01-31
      • 2019-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-07
      • 1970-01-01
      • 1970-01-01
      • 2017-10-30
      相关资源
      最近更新 更多