为什么我的Viewpager在第二次打开后没有显示任何片段,为什么不能使用childfragmentmanager?

Why is my Viewpager not showing any fragments after opening the second time and why can childfragmentmanager not be used?

我在 Viewpager (MainViewPager) 中放置了一个片段 (InnerFragment)。

这个 Fragment (InnerFragment) 也确实包含一个带有多个 Fragment 的 ViewPager (NestedViewPager)。当我滑动或打开 InnerFragment 时,一切正常,NestedViewPager 以应有的方式显示所有片段。

当我在滑动 MainViewPager 后离开 InnerFragment 并返回到 InnerFragment 时,它是空白的,什么也没有显示

互联网上描述的一种解决方案是使用childfragmentmanager。不幸的是,这不起作用,因为如果我这样做 将抛出以下异常

java.lang.IllegalStateException: Fragment ProductImageFragment{c458f99 (1213d869-3715-4541-8nab-f87cyc350630) id=0x8f093165 android:switcher:2111xxxxxx:0} declared target fragment ProductImagesFragment{4b4f25e (99a6aaf6-5500-4821-902f-7bf30f87554c) id=0x7f090126 android:switcher:2131296550:2} that does not belong to this FragmentManager!
        at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:805)

也许还需要注意,MainFragment 正在实现 Innerfragment.

的接口

MainFragment

class ProductImagesFragment : Fragment(),
    ProductImageFragment.IProductImageHandler,
    ProductImageUploadDialog.ProductImageUploadDialogEventListener {

    private lateinit var viewPager: ViewPager

    interface IProductImageUploadHandler{
        fun onImageUploadToServerFinished(bitmap: Bitmap, imageData: ProductImage)
    }

    override fun onCreate(savedInstanceState: Bundle?)
    {
        super.onCreate(savedInstanceState)
    }
   

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View?
    {
        return layoutInflater.inflate(R.layout.fragment_product_images,container,false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        viewPager = view.findViewById(R.id.viewPagerImages)
        setViewPagerAdapter()
    }

    private fun setViewPagerAdapter(){
        val manager = fragmentManager
        if(manager!=null){
            viewPager.adapter = product?.let {product ->
                ImageAdapter(this,
                    product,productImageList,manager)
            }
        }
    }

class ImageAdapter(private val productImagesFragment: ProductImagesFragment, private val product: Product, private val imagesList:ArrayList<ProductImage>, fragmentManager: FragmentManager) : FragmentPagerAdapter(fragmentManager) {

        override fun getCount(): Int {
            return imagesList.size
        }

        override fun getItem(position: Int): Fragment {
            val fragment = ProductImageFragment()
            val bundle = Bundle()
            val image = imagesList[position]
            bundle.putInt("productId",product.id)
            bundle.putParcelable("productImage",image)
            bundle.putInt("maxImages",imagesList.size)
            fragment.setTargetFragment(productImagesFragment,1)
            fragment.arguments = bundle
            return fragment
        }
    }


}

InnerFragment

class ProductImageFragment : Fragment() {

    private lateinit var productImageHandler: IProductImageHandler

    interface IProductImageHandler{
        fun onImageDeleted(id: Int)
        fun onOrderChanged(url: String, newValue: Int, oldValue: Int)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    override fun onAttach(context: Context) {
        super.onAttach(context)
        try {
            productImageHandler = targetFragment as IProductImageHandler
        }catch (exception: Exception){
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_product_image, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
       
    }

    private fun setValues(view: View){
        
    }
}

必须使用childFragmentManager才能正确恢复状态。

您根本不应该使用目标片段 API。事实上,如果您使用 childFragmentManager,您的子片段已经通过 requireParentFragment() 引用了父片段 - 只需使用它而不是 targetFragment

override fun onAttach(context: Context) {
    super.onAttach(context)
    try {
         productImageHandler = requireParentFragment() as IProductImageHandler
    }catch (exception: Exception){
    }
}