如何获取FragmentTabHost中指定标签页的Fragment
2个回答
展开全部
//定义FragmentTabHost对象
private MyFragmentTabHost mTabHost;
//定义数组来存放Fragment界面
private Class fragmentArray[] = {BaseFragment.class,BaseFragment.class,BaseFragment.class,BaseFragment.class,BaseFragment.class};
//Tab选项卡的文字
private String mTextviewArray[] = {"a", "b", "c", "d", "e"};
//实例化TabHost对象,得到TabHost
mTabHost = (SunnyFragmentTabHost)findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
//得到fragment的个数
int count = fragmentArray.length;
for(int i = 0; i < count; i++){
//为每一个Tab按钮设置图标、文字和内容
TabHost.TabSpec tabSpec = mTabHost.newTabSpec(mTextviewArray[i]).setIndicator(getTabItemView(i));
//将Tab按钮添加进Tab选项卡中
mTabHost.addTab(tabSpec, fragmentArray[i], null);
//设置Fragment的背景
BaseFragment bf = (BaseFragment)getSupportFragmentManager().findFragmentByTag(mTextviewArray[i]);
if(bf!=null)
bf.setBackgroundResource(mBgImgs[i]);
}
其中MyFragmentTabHost参考http://blog.csdn.net/jwzhangjie/article/details/35285779编写,主要是detach和attach部分改为了hide和show
然后在addTab(TabHost.TabSpec tabSpec, Class<?> clss, Bundle args)函数最后增加:
if(info.fragment==null){
info.fragment = Fragment.instantiate(mContext,
info.clss.getName(), info.args);
FragmentTransaction ft = mFragmentManager.beginTransaction();
ft.add(mContainerId, info.fragment, info.tag);
ft.hide(info.fragment);
ft.commit();
}
private MyFragmentTabHost mTabHost;
//定义数组来存放Fragment界面
private Class fragmentArray[] = {BaseFragment.class,BaseFragment.class,BaseFragment.class,BaseFragment.class,BaseFragment.class};
//Tab选项卡的文字
private String mTextviewArray[] = {"a", "b", "c", "d", "e"};
//实例化TabHost对象,得到TabHost
mTabHost = (SunnyFragmentTabHost)findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
//得到fragment的个数
int count = fragmentArray.length;
for(int i = 0; i < count; i++){
//为每一个Tab按钮设置图标、文字和内容
TabHost.TabSpec tabSpec = mTabHost.newTabSpec(mTextviewArray[i]).setIndicator(getTabItemView(i));
//将Tab按钮添加进Tab选项卡中
mTabHost.addTab(tabSpec, fragmentArray[i], null);
//设置Fragment的背景
BaseFragment bf = (BaseFragment)getSupportFragmentManager().findFragmentByTag(mTextviewArray[i]);
if(bf!=null)
bf.setBackgroundResource(mBgImgs[i]);
}
其中MyFragmentTabHost参考http://blog.csdn.net/jwzhangjie/article/details/35285779编写,主要是detach和attach部分改为了hide和show
然后在addTab(TabHost.TabSpec tabSpec, Class<?> clss, Bundle args)函数最后增加:
if(info.fragment==null){
info.fragment = Fragment.instantiate(mContext,
info.clss.getName(), info.args);
FragmentTransaction ft = mFragmentManager.beginTransaction();
ft.add(mContainerId, info.fragment, info.tag);
ft.hide(info.fragment);
ft.commit();
}
展开全部
使用FragmentTabHost构建了包含几个标签页的界面,如何获取指定标签页的Fragment?
How to get Fragment in FragmentTabHost?
TabHost中Fragment的Tag
一般通过FragmentTabHost.addTab(TabSpec tabSpec, Class<?> clss, Bundle args)方法添加fragment:
TabSpec tabSpec = mTabHost.newTabSpec("TAB_TAG_" + i).setIndicator("TAB_TITLE_" + i);
mTabHost.addTab(tabSpec, MyFragment.class, null);
而方法TabHost.TabSpec newTabSpec(String tag)的参数tag就是fragment的tag.
那么,我们有2种方法获取fragment。
通过Fragment.getTag()获取Fragment
最开始采用了这种办法,首先获取fragment manager管理的fragment列表,然后根据tag从列表中查找fragment:
private Fragment getFragment(int tabId)
{
List<Fragment> fragments = getSupportFragmentManager().getFragments();
for(Fragment fragment : fragments) {
String str1 = fragment.getTag();
String str2 = String.valueOf("TAB_TAG_" + tabId);
if(str1 != null && str1.equals(str2)) // 最开始没有检查str1是否为空,导致crash!
return fragment;
}
return null;
}
然后掉进坑里了!
因为拿到的fragment列表,不仅仅是TabHost包含的fragment,还包含向attached activity添加的其它fragment,而如果这些fragment并未设置tag,那么fragment.getTag()将返回null,然后就crash了。
通过FragmentManager.findFragmentByTag(String tag)获取Fragment
感觉这个方法最简洁。
private Fragment getFragment(int tabId)
{
return getSupportFragmentManager().findFragmentByTag("TAB_TAG_" + tabId);
}
在onTabChanged()中获取的fragment有时为空
打印了一些log发现,
如果第1次切换到某个标签页,在onTabChanged()方法中立刻调用findFragmentByTag()时,返回的总是null,但如果延迟一段时间,就一定可以获取fragment.
如果再次切换到某个标签页,在onTabChanged()中不需要延时,总可以返回fragment.
@Override
public void onTabChanged(final String tabId)
{
Fragment fg = getSupportFragmentManager().findFragmentByTag(tabId);
Log.d(TAG, "onTabChanged(): " + tabId + ", fragment " + fg);
if (fg == null) {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Fragment fg = getSupportFragmentManager().findFragmentByTag(tabId);
Log.d(TAG, "onTabChanged() delay 50ms: " + tabId + ", fragment " + fg);
}
}, 50);
}
}
LogCat输出:
// cannot get the selected fragment immediately if the fragment has never been instantiated.
onTabChanged(): 1, fragment null
onTabChanged() delay 50ms: 1, fragment HistoryFragment{6f7a9d5 #2 id=0x7f09006e 1}
onTabChanged(): 2, fragment null
onTabChanged() delay 50ms: 2, fragment HistoryFragment{10c59e72 #3 id=0x7f09006e 2}
// can get the selected fragment immediately if the fragment already instantiated.
onTabChanged(): 1, fragment HistoryFragment{6f7a9d5 #2 id=0x7f09006e 1}
onTabChanged(): 2, fragment HistoryFragment{10c59e72 #3 id=0x7f09006e 2}
How to get Fragment in FragmentTabHost?
TabHost中Fragment的Tag
一般通过FragmentTabHost.addTab(TabSpec tabSpec, Class<?> clss, Bundle args)方法添加fragment:
TabSpec tabSpec = mTabHost.newTabSpec("TAB_TAG_" + i).setIndicator("TAB_TITLE_" + i);
mTabHost.addTab(tabSpec, MyFragment.class, null);
而方法TabHost.TabSpec newTabSpec(String tag)的参数tag就是fragment的tag.
那么,我们有2种方法获取fragment。
通过Fragment.getTag()获取Fragment
最开始采用了这种办法,首先获取fragment manager管理的fragment列表,然后根据tag从列表中查找fragment:
private Fragment getFragment(int tabId)
{
List<Fragment> fragments = getSupportFragmentManager().getFragments();
for(Fragment fragment : fragments) {
String str1 = fragment.getTag();
String str2 = String.valueOf("TAB_TAG_" + tabId);
if(str1 != null && str1.equals(str2)) // 最开始没有检查str1是否为空,导致crash!
return fragment;
}
return null;
}
然后掉进坑里了!
因为拿到的fragment列表,不仅仅是TabHost包含的fragment,还包含向attached activity添加的其它fragment,而如果这些fragment并未设置tag,那么fragment.getTag()将返回null,然后就crash了。
通过FragmentManager.findFragmentByTag(String tag)获取Fragment
感觉这个方法最简洁。
private Fragment getFragment(int tabId)
{
return getSupportFragmentManager().findFragmentByTag("TAB_TAG_" + tabId);
}
在onTabChanged()中获取的fragment有时为空
打印了一些log发现,
如果第1次切换到某个标签页,在onTabChanged()方法中立刻调用findFragmentByTag()时,返回的总是null,但如果延迟一段时间,就一定可以获取fragment.
如果再次切换到某个标签页,在onTabChanged()中不需要延时,总可以返回fragment.
@Override
public void onTabChanged(final String tabId)
{
Fragment fg = getSupportFragmentManager().findFragmentByTag(tabId);
Log.d(TAG, "onTabChanged(): " + tabId + ", fragment " + fg);
if (fg == null) {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Fragment fg = getSupportFragmentManager().findFragmentByTag(tabId);
Log.d(TAG, "onTabChanged() delay 50ms: " + tabId + ", fragment " + fg);
}
}, 50);
}
}
LogCat输出:
// cannot get the selected fragment immediately if the fragment has never been instantiated.
onTabChanged(): 1, fragment null
onTabChanged() delay 50ms: 1, fragment HistoryFragment{6f7a9d5 #2 id=0x7f09006e 1}
onTabChanged(): 2, fragment null
onTabChanged() delay 50ms: 2, fragment HistoryFragment{10c59e72 #3 id=0x7f09006e 2}
// can get the selected fragment immediately if the fragment already instantiated.
onTabChanged(): 1, fragment HistoryFragment{6f7a9d5 #2 id=0x7f09006e 1}
onTabChanged(): 2, fragment HistoryFragment{10c59e72 #3 id=0x7f09006e 2}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询