FragmentManager,LocalActivityManager和TabHost.setup() [英] FragmentManager, LocalActivityManager and TabHost.setup()
问题描述
好吧,我有从FragmentManager继承的类。这个类中我有创造一些片段选项卡,在我TabHost插入FragmentPagerAdapter。在code编译罚款和作品。我把下面论文类的某些部分。我使用的是做这个部份的Android一如既往支持包V4
Ok, I have a class that inherits from FragmentManager. Inside this class I have a FragmentPagerAdapter that create some fragment tabs and insert in my TabHost. The code compiles fine and works. I put some parts of theses classes below. I'm using to do this ths android supprt package v4
public class TabMenu extends FragmentActivity implements TabHost.OnTabChangeListener, ViewPager.OnPageChangeListener{
...
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.tabs_container);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
tabHost = (TabHost)findViewById(android.R.id.tabhost);
tabHost.setup();
viewPager = (ViewPager)findViewById(R.id.viewpager);
...
menuAdapter = new MenuPagerAdapter(this, tabHost, viewPager);
menuAdapter.addTab("meutime", "Meu Time", MeuTime.class, extras);
...
tabHost.setOnTabChangedListener(this);
viewPager.setOnPageChangeListener(this);
...
public class MenuPagerAdapter extends FragmentPagerAdapter{
public MenuPagerAdapter(FragmentActivity fragmentActivity, TabHost tabHost, ViewPager viewPager) {
...
}
@Override
public Fragment getItem(int position) {
String currentFragment = tabNames.get(position);
return fragmentManager.findFragmentByTag(currentFragment);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return tabNames.size();
}
public void addTab(String tag, String tabLabel, Class<?> cls, Bundle bundle){
TabHost.TabSpec tabSpec = tabHost.newTabSpec(tag).setIndicator(createTab(tabLabel));
Intent intent = new Intent();
intent.setClass(context, cls);
intent.putExtras(bundle);
tabSpec.setContent(intent);
tabNames.add(tag);
tabHost.addTab(tabSpec);
notifyDataSetChanged();
}
private View createTab(String tabLabel){
View view = LayoutInflater.from(context).inflate(R.layout.tab_spec_layout, null, false);
...
return view;
}
}
当我实例化这个FragmentActivity我收到了以下异常:
When I instantiate this FragmentActivity I received the following exception:
java.lang.IllegalStateException: Did you forget to call 'public void setup(LocalActivityManager activityGroup)'?
阅读Android开发者网站的LocalActivityManager是pcated德$ P $。但它是在TabHost.setup(LocalActivityManager)使用。
如果这个类是德precated,是有这种情况的解决方案?我不能使用TabContentFactory。
Reading on Android developer' site the LocalActivityManager is deprecated. But it is used in TabHost.setup(LocalActivityManager). If this class is deprecated, is there a solution for this case? I can't use TabContentFactory.
推荐答案
Unhapilly我用TabContentFactory。我个人我以前不喜欢这个解决方案来创建一个空的观点,但工作。
Unhapilly I've used TabContentFactory. Personally I did't like this solution to create a empty view but works.
public void addTab(String tag, final String tabLabel, Class<?> cls, Bundle bundle){
TabHost.TabSpec tabSpec = tabHost.newTabSpec(tag).setIndicator(createTab(tabLabel));
tabSpec.setContent(new TabHost.TabContentFactory() {
public View createTabContent(String tag) {
View view = new View(context);
view.setMinimumHeight(0);
view.setMinimumWidth(0);
return view;
}
});
fragContentInstances.add(new FragContentInfos(cls, bundle));
tabHost.addTab(tabSpec);
notifyDataSetChanged();
}
private class FragContentInfos{
private Class<?> cls;
private Bundle bundle;
public FragContentInfos(Class<?> cls, Bundle bundle){
this.cls = cls;
this.bundle = bundle;
}
}
这篇关于FragmentManager,LocalActivityManager和TabHost.setup()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!