<bdo id='Hhhzz'></bdo><ul id='Hhhzz'></ul>

      <small id='Hhhzz'></small><noframes id='Hhhzz'>

        <legend id='Hhhzz'><style id='Hhhzz'><dir id='Hhhzz'><q id='Hhhzz'></q></dir></style></legend>

        <i id='Hhhzz'><tr id='Hhhzz'><dt id='Hhhzz'><q id='Hhhzz'><span id='Hhhzz'><b id='Hhhzz'><form id='Hhhzz'><ins id='Hhhzz'></ins><ul id='Hhhzz'></ul><sub id='Hhhzz'></sub></form><legend id='Hhhzz'></legend><bdo id='Hhhzz'><pre id='Hhhzz'><center id='Hhhzz'></center></pre></bdo></b><th id='Hhhzz'></th></span></q></dt></tr></i><div id='Hhhzz'><tfoot id='Hhhzz'></tfoot><dl id='Hhhzz'><fieldset id='Hhhzz'></fieldset></dl></div>
        <tfoot id='Hhhzz'></tfoot>

      1. 来自 viewpager2 和我的父片段的 SharedViewModel

        时间:2023-10-04

        <tfoot id='3XMB5'></tfoot>

      2. <legend id='3XMB5'><style id='3XMB5'><dir id='3XMB5'><q id='3XMB5'></q></dir></style></legend>
          <tbody id='3XMB5'></tbody>
        <i id='3XMB5'><tr id='3XMB5'><dt id='3XMB5'><q id='3XMB5'><span id='3XMB5'><b id='3XMB5'><form id='3XMB5'><ins id='3XMB5'></ins><ul id='3XMB5'></ul><sub id='3XMB5'></sub></form><legend id='3XMB5'></legend><bdo id='3XMB5'><pre id='3XMB5'><center id='3XMB5'></center></pre></bdo></b><th id='3XMB5'></th></span></q></dt></tr></i><div id='3XMB5'><tfoot id='3XMB5'></tfoot><dl id='3XMB5'><fieldset id='3XMB5'></fieldset></dl></div>
          <bdo id='3XMB5'></bdo><ul id='3XMB5'></ul>
          1. <small id='3XMB5'></small><noframes id='3XMB5'>

                  本文介绍了来自 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()
                  
                  }
                  

                  现在,在 Fragment 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,它位于视图页面的 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 共享到 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()})
                  
                  }
                  

                  我收到以下错误

                  java.lang.IllegalStateException: Fragment FragmentProducts{6953da8}(3a2f5cbb-113d-4ed2-8f85-56e04fcea356) f0} 不是子片段,它直接附在com.example.MainActivity@dcc2679

                  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

                  所以,这告诉我 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).不要那样做.将您的父 Fragment 传递给 MyViewPageAdapter 并将 that 传递给 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:android回收器视图中的水平和垂直滚动 下一篇:我可以在 Android 的 Viewpager 中使用 Horizontal Scrollview 吗?

                  相关文章

                    <tfoot id='dUidz'></tfoot>

                        <bdo id='dUidz'></bdo><ul id='dUidz'></ul>
                      <legend id='dUidz'><style id='dUidz'><dir id='dUidz'><q id='dUidz'></q></dir></style></legend>
                    1. <i id='dUidz'><tr id='dUidz'><dt id='dUidz'><q id='dUidz'><span id='dUidz'><b id='dUidz'><form id='dUidz'><ins id='dUidz'></ins><ul id='dUidz'></ul><sub id='dUidz'></sub></form><legend id='dUidz'></legend><bdo id='dUidz'><pre id='dUidz'><center id='dUidz'></center></pre></bdo></b><th id='dUidz'></th></span></q></dt></tr></i><div id='dUidz'><tfoot id='dUidz'></tfoot><dl id='dUidz'><fieldset id='dUidz'></fieldset></dl></div>
                    2. <small id='dUidz'></small><noframes id='dUidz'>