Communication between ParentFragment and Fragment in ViewPager
Asked Answered
P

1

0

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

Pilsner answered 10/5, 2019 at 14:17 Comment(1)
any hep ?......Pilsner
P
0

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

Pilsner answered 11/5, 2019 at 12:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.