从 Viewpager2 片段访问父片段函数

Accessing Parent's Fragment function from Viewpager2 fragment

我正在使用导航组件,在我的主页片段中有一个 ViewPager2。我们知道 viewPager2 也包含 Fragment。如何通过viewpager2的片段调用父函数?我尝试了一些方法,比如在构造函数中传递函数,但我意识到使用单例模式创建的片段。我只知道如何在构造函数中传递函数,例如标准 class (Not Singleton class)

我的适配器

class DetailSliderAdapter(fragmentActivity: FragmentActivity) : FragmentStateAdapter(fragmentActivity) {

    private val images = mutableListOf<String>()

    fun addImages(lsImage: List<String>){
        images.addAll(lsImage)
        notifyDataSetChanged()
    }

    override fun getItemCount(): Int = images.size
    override fun createFragment(position: Int): Fragment {
        return SliderImageFragment.newInstance(images[position])
    }

    class SliderImageFragment: Fragment(){
        companion object{
            const val EXTRA_SLIDER_IMAGE = "extra_slider_image"
            fun newInstance(imageStr: String) = SliderImageFragment().apply {
                this.arguments = Bundle().apply {
                    this.putString(EXTRA_SLIDER_IMAGE, imageStr)
                }
            }

        }

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

        override fun onActivityCreated(savedInstanceState: Bundle?) {
            super.onActivityCreated(savedInstanceState)

            val image = arguments?.getString(EXTRA_SLIDER_IMAGE) as? String

            image?.let {
                Picasso.get()
                    .load(image)
                    .into(image_slide)

                image_slide.setOnClickListener {
                    
                    // here i want to call parent's Fragment  function
              
            }
        }
    }
}

我的碎片之家

class DetailFragment : Fragment() {
    private var root: View ?= null
    private var viewpagerSlider : ViewPager2 ?= null

    private val viewModel : DetailViewModel by lazy {
        ViewModelProviders.of(this, factory).get(DetailViewModel::class.java)
    }

    private val adapterSlider : DetailSliderAdapter by lazy {
        DetailSliderAdapter(requireActivity()){
            //listener
        }
    }

    fun clearAllAdapter(){
        // NEED TO CALL THIS
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val safeArgs : DetailFragmentArgs by navArgs<DetailFragmentArgs>()
        idProduct = safeArgs.idProduct
        Timber.d("ID PRODUCT $idProduct")

        if(root == null){
            root = inflater.inflate(R.layout.fragment_detail, container, false)
            idProduct?.let {
                viewModel.getDetail(idProduct!!)
            }
        }
        return root
    }


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

        Timber.d("CALL onViewCreated")

        viewModel.getDataDetail().observe(viewLifecycleOwner, Observer {
            when(it.status){
                
                Response.ResponseStatus.LOADED -> {
                    progress_circular.visibility = View.GONE
                    val data = it.data
                    data?.let { d->
                        val detailProduct = d.detailProduct
                        detailProduct?.let { pd->
                            updateUI(pd, data)
                        }

                    }
                }
               
            }

        })
    }

    private fun updateUI(pd: ProductDetail, data: Data) {
        pd.image?.let {
            updateSliderPhoto(pd.image)
        }
    }

    private fun updateSliderPhoto(
        image: List<String>
    ) {
        viewpagerSlider = root?.findViewById(R.id.slider_photo)
        adapterSlider.addImages(image)

        viewpagerSlider?.apply {
            this.adapter = adapterSlider
            this.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback(){
                override fun onPageSelected(position: Int) {
                    super.onPageSelected(position)
                    page_indicator.text = "${(position+1)}/${image.size}"
                }
            })
        }
    }

}

你应该 永远不要Fragment 中将 FragmentActivity 传递给你的 FragmentStateAdapter - 这就是为什么有 FragmentStateAdapter 采用 Fragment 的构造函数。只有 Fragment 构造函数正确地将 ViewPager2 片段嵌套在您的父片段中。

使用正确的构造函数后,您的 SliderImageFragment 可以调用 requireParentFragment() 并将其转换为 DetailFragment 并调用您的方法。