Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SharedViewModel from viewpager2 and my parent Fragment

So, I have one fragment, lets call it Fragment A, in which I need it to be the main host of my sharedviewmodel.

So I have it like this

class FragmentA:Fragment() {

 private val model: SharedViewModel by viewModels()

}

Now, inside Fragment A, I set viewpager2 Adapter

private fun setupViewPagerWithTabLayout(
        productList: MutableList<Producto>,
        drinkList: MutableList<Producto>
    ) {
        val fragmentList = listOf(
            FragmentProducts.newInstance(productList),
            FragmentProducts.newInstance(drinkList))

        viewPager2.adapter = MyViewPageAdapter(requireActivity(),fragmentList)
        val tabLayoutMediator = TabLayoutMediator(tabLayout, viewPager2,
            TabLayoutMediator.TabConfigurationStrategy { tab, position ->

                when (position) {
                    0 -> {
                        tab.text = "Option1"
                    }
                    1 -> {
                        tab.text = "Option2"
                    }
                }

            })
        tabLayoutMediator.attach()
    }

Here I instantiate my FragmentProduct which is inside Fragment A in a viewpager, but my adapter needs a FragmentActivity

class MyViewPageAdapter(fragmentActivity: FragmentActivity,private val fragmentList: List<FragmentProducts>) :
    FragmentStateAdapter(fragmentActivity) {

    override fun getItemCount(): Int {
        return fragmentList.size
    }

    override fun createFragment(position: Int): Fragment {
        return fragmentList[position]
    }
}

So, from FragmentProducts that is contained inside FragmentA, I need to share data with this viewmodel, but when I do this to share data from this fragment to FragmentA

class FragmentProducts:Fragment(){

    private val model: SharedViewModel by viewModels ({requireParentFragment()})

}

I get the followin error

java.lang.IllegalStateException: Fragment FragmentProducts{6953da8} (3a2f5cbb-113d-4ed2-8f85-56e04fcea356) f0} is not a child Fragment, it is directly attached to com.example.MainActivity@dcc2679

so , this is telling me that FragmentProducts references to my MainActivity, and thats logic because the instance of the adapter takes requireActivity() for FragmentActivity at

viewPager2.adapter = MyViewPageAdapter(requireActivity(),fragmentList)

So, I need to pass instead of requireActivity() my FragmentA as the host of this viewpager fragment but at my ViewPagerAdapter, I cant pass FragmentStatePagerAdapter or something else to pass my FragmentA as the context for that viewpager

Since Im using viewpager2, is there a way to let FragmentA host FragmentProducts viewpager so I can get it in my FragmentProducts like this

private val model: SharedViewModel by viewModels ({requireParentFragment()})

Thanks

like image 576
SNM Avatar asked Nov 28 '22 21:11

SNM


1 Answers

You're using FragmentStateAdapter(fragmentActivity). Don't do that. Pass your parent Fragment to MyViewPageAdapter and pass that to FragmentStateAdapter.

class MyViewPageAdapter(
    parentFragment: Fragment,
    private val fragmentList: List<FragmentProducts>
) : FragmentStateAdapter(parentFragment) {
like image 66
ianhanniballake Avatar answered Dec 06 '22 11:12

ianhanniballake