如何初始化(参数传递)片段的动作栏选项卡? [英] how to initialize(pass arguments) fragments for action bar tabs?

查看:139
本文介绍了如何初始化(参数传递)片段的动作栏选项卡?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我跟着 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()来任意对象链接到的标签。

myVariableList 到捆绑,您可以通过以下步骤实现了简单的解决方案>

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()

这篇关于如何初始化(参数传递)片段的动作栏选项卡?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆