问题
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