如何在FragmentTabHost添加tab后获取Fragment实例
1个回答
2016-10-13 · 百度知道合伙人官方认证企业
兄弟连教育
兄弟连教育成立于2006年,11年来专注IT职业教育,是国内专业的IT技术培训学校。2016年成功挂牌新三板(股票代码:839467)市值过亿。开设专注程序员培训专注php、Java、UI、云计算、Python、HTML5、
向TA提问
关注
展开全部
//定义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]);
}
其亏察中MyFragmentTabHos主要是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]);
}
其亏察中MyFragmentTabHos主要是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();
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询