在一个FragmentTabHost片段沟通 [英] Communicate with a fragment in a FragmentTabHost
本文介绍了在一个FragmentTabHost片段沟通的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在FragmentTabHost片段通讯
I want to communicate with a fragment in a FragmentTabHost
通信Fragment->活动完成了!与接口
The communication Fragment->Activity is done! With an interface.
但我不能创造一个通信活动 - >片段,因为我创建的片段是这样的:
But I can't create a communication Activity->Fragment because I created the fragment like this:
mTabHost.addTab(
mTabHost.newTabSpec("tab2").setIndicator("Affichage",
getResources().getDrawable(android.R.drawable.star_on)),
MySelectionFragment.class, null);
MySelectionFragment是一类并不像是一个片段新MySelectionFragment()
和我不知道如何与类通讯:/
And I dunno how to communicate with a class :/
在此先感谢!
推荐答案
诀窍是覆盖这样的onAttach方法:
The trick was to override the onAttach method like this:
@Override
public void onAttachFragment(android.support.v4.app.Fragment attachedFragment) {
super.onAttachFragment(attachedFragment);
if (attachedFragment.getClass().equals((ObjectA.class)) {
mObjectA = (ObjectA)attachedFragment;
}
if (attachedFragment.getClass().equals((ObjectB.class)) {
mObjectB = (ObjectB) attachedFragment;
}
}
这篇关于在一个FragmentTabHost片段沟通的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文