Reputation: 21
See my code
I am using viewpager 2 in a fragment to hold child fragments. There can be 'n' number of child fragments. It is showing lint error in adapter.
//See my fragment code
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)
}
// my adapter code
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])
}
The lint error is in the following line of the adapter class
class DocAdapter(fa: FragmentManager, lifecycle: Lifecycle, private val docs : MutableList<Int>) :
FragmentStateAdapter(fa) {
Upvotes: 2
Views: 1396
Reputation: 20684
FragmentStateAdapter
requires a FragmentManager
and Lifecycle
in its constructor. Change your DocAdapter
to this:
class DocAdapter(fa: FragmentManager, lifecycle: Lifecycle, private val docs : MutableList<Int>) :
FragmentStateAdapter(fa, lifecycle) {
...
}
Upvotes: 2