findFragmentByTag - 寻找片段FragmentTabHost - 总是空 [英] findFragmentByTag - looking for fragment in FragmentTabHost - always null
问题描述
我遇到了麻烦指向一个片段是一个FragmentTabhost当前可见的片段。
I'm having trouble getting a pointer to a Fragment which is the currently visible fragment in a FragmentTabhost.
我有一个 SherlockFragmentActivity
名为 SecondActivity
加载的Tabhost从它的的onCreate
这样的方法:
I have a SherlockFragmentActivity
called SecondActivity
that loads the Tabhost from it's onCreate
method like this:
if (getSupportFragmentManager().findFragmentById(android.R.id.content) == null) {
Fragment f = new TabsFragment();
getSupportFragmentManager().beginTransaction().add(android.R.id.content, f, "tabsfragment").commit();
}
TabsFragment
是 SherlockFragment
子类,这个的onCreate
方法来创建标签
TabsFragment
is a SherlockFragment
subclass with this onCreate
method to create the tabs
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mTabHost = new FragmentTabHost(getActivity());
mTabHost.setup(getActivity(), getChildFragmentManager(), R.layout.tabs);
mTabHost.addTab(mTabHost.newTabSpec("Tab1").setIndicator("Offers",
getResources().getDrawable(R.drawable.offersale)),
OfferListFragment.class,
null);
mTabHost.addTab(mTabHost.newTabSpec("Tab2").setIndicator("News",
getResources().getDrawable(R.drawable.newspaper)),
NewsFragment.class,
null);
return mTabHost;
}
现在,当我在第二个选项卡,我在用原来的活动 SecondActivity
,然后我把这个也就是启动的类来完成后台任务应该给我一个参考标签,但它总是返回null!
Now when i'm in the 2nd tab, I have a background task done in a class that is initiated by the original activity SecondActivity
, then I call this which is supposed to give me a reference to the tab, but it always returns null!
NewsFragment新闻浏览=(NewsFragment)delegate.getSupportFragmentManager()findFragmentByTag(TAB2);
在代理
变量是一个指针回 SecondActivity
时,启动背景类。
The delegate
variable is a pointer back to SecondActivity
when it starts the background class.
我如何获得一个指针选项卡的片段?
我错了TAB2设定的时候添加的标签是标签
的片段?
How do I get a pointer to the tab's fragment?
Am I wrong that "Tab2" set when adding the tabs is the Tag
for the fragment?
推荐答案
我真的不喜欢回答我自己的问题,但它是惊人的什么睡觉就可以做。 这个怪物给我一个指针片段在tabhost
I don't really like answering my own questions, but it's amazing what sleeping on it can do. This monster gives me a pointer to the fragment in the tabhost
NewsFragment newsView = (NewsFragment) delegate
.getSupportFragmentManager()
.findFragmentByTag("tabsfragment")
.getChildFragmentManager()
.findFragmentByTag("Tab2");
这篇关于findFragmentByTag - 寻找片段FragmentTabHost - 总是空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!