本文介绍了来自viewpager2和我的父片段的SharedViewModel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我有一个片段,我们称其为片段A,在其中我需要它作为我的sharedviewmodel的主要宿主.

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

所以我有这样

class FragmentA:Fragment() {

 private val model: SharedViewModel by viewModels()

}

现在,在片段A内,我设置了viewpager2适配器

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()
    }

在这里我实例化我的FragmentProduct,它位于viewpager中Fragment A的内部,但是我的适配器需要FragmentActivity

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]
    }
}

因此,从FragmentA中包含的FragmentProducts中,我需要与此视图模型共享数据,但是当我执行此操作以将数据从该片段共享到FragmentA时

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()})

}

我收到关注错误

所以,这告诉我FragmentProducts引用了我的MainActivity,这就是逻辑,因为适配器的实例在FragmentActivity处需要requireActivity()

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)

因此,我需要传递FragmentA而不是requireActivity()作为该viewpager片段的宿主,但是在我的ViewPagerAdapter上,我无法传递FragmentStatePagerAdapter或其他东西来传递FragmentA作为该viewpager的上下文

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

自从我使用viewpager2以来,有没有办法让FragmentA托管FragmentProducts viewpager,这样我就可以在我的FragmentProducts中获取它

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()})

谢谢

推荐答案

您正在使用FragmentStateAdapter(fragmentActivity).不要那样做将您的父片段传递给MyViewPageAdapter,并将那个传递给FragmentStateAdapter.

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

这篇关于来自viewpager2和我的父片段的SharedViewModel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-10 03:06