SharedViewModel from viewpager2 and my parent Fragment

不打扰是莪最后的温柔 提交于 2020-03-25 17:56:31

问题


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


回答1:


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) {


来源:https://stackoverflow.com/questions/60698699/sharedviewmodel-from-viewpager2-and-my-parent-fragment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!