本文介绍了ViewPager中ParentFragment和Fragment之间的通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个MainActivity
持有HomeFragment
HomeFragment
具有sort
按钮Viewpager
,其中包含2个片段
HomeFragment
has sort
button Viewpager
which holds 2 fragments
在HomeFragment
中单击sort
按钮时,必须将值传递给ViewPager Fragmensts
when sort
button is clicked in HomeFragment
, the value has to be passed to ViewPager Fragmensts
我在google上找到了直接位于MainActivity中的Fragments之间的通讯
I google where I found communication between Fragments which is placed directly in MainActivity
推荐答案
我试图借助stackoverflow自己解决此问题
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
}
注意:此SortListener
在ChildFragment中实现了
Note: implemented this SortListener
in ChildFragment
这篇关于ViewPager中ParentFragment和Fragment之间的通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!