问题描述
我跟着 http://developer.android.com/guide/主题/ UI / actionbar.html#标签
它使用以下code添加标签。
It uses the following code to add Tab.
Tab tab = actionBar.newTab()
.setText(R.string.artist)
.setTabListener(new TabListener<ArtistFragment>(
this, "artist", ArtistFragment.class));
actionBar.addTab(tab);
我要提供参数片段的构造函数或显示在第一次的标签之前呼吁片段实例myInit(myVariableList)方法。
I want to supply argument to the fragment's constructor or call myInit(myVariableList) method on the fragment instance before showing the tab for the first time.
我怎么能这样做?
推荐答案
您可以使用 tab.setTag()
来任意对象链接到的标签。
捆绑
,您可以通过以下步骤实现了简单的解决方案>You can use tab.setTag()
to link an arbitrary object to the tab. If you can put myVariableList
into a Bundle
, you can achieve a simple solution by doing the following --
Tab tab = actionBar.newTab()
.setText(R.string.artist)
.setTabListener(new TabListener<ArtistFragment>(
this, "artist", ArtistFragment.class));
tab.setTag(myVariableBundle);
actionBar.addTab(tab);
然后,在你的 onTabSelected
回调,发送捆绑
当你实例化片段 -
Then, in your onTabSelected
callback, send the Bundle
when you instantiate your fragment --
mFragment = Fragment.instantiate(mActivity, mClass.getName(), (Bundle) tab.getTag());
您应该然后能够访问你的捆绑
使用片段生命周期中 getArguments()
You should then be able to access your Bundle
during the fragment lifecycle using getArguments()
这篇关于如何初始化(参数传递)片段的动作栏选项卡?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!