Communication between ParentFragment and Fragment in ViewPager

淺唱寂寞╮ 提交于 2019-12-02 17:52:51

问题


I have a MainActivity which holds HomeFragment

HomeFragment has sort button Viewpager which holds 2 fragments

when sort button is clicked in HomeFragment, the value has to be passed to ViewPager Fragmensts

I google where I found communication between Fragments which is placed directly in MainActivity


回答1:


I tried to solve this by myself with the help of stackoverflow

HomeFragment.kt

    private var sort: Int = 0
    private var sortListener: SortListener? = null


    override fun onActivityCreated(savedInstanceState: Bundle?) {
            super.onActivityCreated(savedInstanceState)
            val childFragment = adapter?.instantiateItem(viewPager, fragmentPosition) as ChildFragment
            setSortListener(childFragment)
        }

    fun userClickinSort(){
sortListener?.onSorted(HomeConstants.SORT_TYPE_NAME)
}
    private fun setSortListener(sortListener: SortListener) {
            this.sortListener = sortListener
        }

Note: implemented this SortListener in ChildFragment



来源:https://stackoverflow.com/questions/56079502/communication-between-parentfragment-and-fragment-in-viewpager

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