Android的 - 创建一个通用TabHost,传递信息使用捆绑的问题 [英] Android - creating a Generic TabHost, passing info using Bundle problem
问题描述
我有一个项目中,我试图做我现有TabHosts一些重构。我有一堆看起来像下面的类很简单TabHost文件。有的居然只有一个选项卡中,有3,等 - 因此,在他们的唯一真正的区别在于标签的数量和每一个加载的活动类。我想只要创建一个单一的TabHost,可以得到的信息出了在包传递到确定有多少选项卡和所需的信息(规格,指标,内容)建立/添加每个选项卡。好像我可以在包的地方是pretty的基本的和我不熟悉的Parcelable或序列化功能的项目。有什么建议?
公共类SomeTabHost
扩展的ActivityGroup
{
@覆盖
公共无效的onCreate(包savedInstanceState)
{
super.onCreate(savedInstanceState);
//充气自己到布局ViewStub
ViewStub VS =(ViewStub)findViewById(R.id.theViewStub);
vs.setLayoutResource(R.layout.my_tabhost);
vs.inflate();
TabHost主机=(TabHost)findViewById(android.R.id.tabhost);
host.setup(getLocalActivityManager());
host.addTab(host.newTabSpec(TAB1)
.setIndicator(TAB1)
.setContent(新意图(这一点,SomeActivity.class)));
host.addTab(host.newTabSpec(TAB2)
.setIndicator(TAB2)
.setContent(新意图(这一点,SomeOtherActivity.class)));
INT numChildren的= host.getTabWidget()getChildCount()。
的for(int i = 0; I< numChildren的;我++)
{
。host.getTabWidget()getChildAt(我).getLayoutParams()高度= 35。
}
} //结束的onCreate
} //结束类
看起来像我跳问这个问题还为时过早。最后我用Serializable类实现解决我的问题。 Hopefuly别人认为这是有用的。请参见下面的code:
首先创建的类来保存数据。
公共类TabDetails实现Serializable
{
私有静态最后长的serialVersionUID = 1L;
公共字符串则tabspec =;
公共字符串tabIndicator =;
公共类<> tabContent = NULL;
公共TabDetails(字符串aTabSpec,
字符串aTabIndicator,
类<> aTabContent)
{
this.tabSpec = aTabSpec;
this.tabIndicator = aTabIndicator;
this.tabContent = aTabContent;
}
} //结束类
然后更新了通用标签主机
公共类GenericTabHost扩展的ActivityGroup
{
公共静态最后弦乐TABS =标签;
@覆盖
公共无效的onCreate(包savedInstanceState)
{
super.onCreate(savedInstanceState);
//充气自己到布局ViewStub
ViewStub VS =(ViewStub)findViewById(R.id.theViewStub);
vs.setLayoutResource(R.layout.mc_tabhost);
vs.inflate();
TabHost主机=(TabHost)findViewById(android.R.id.tabhost);
host.setup(getLocalActivityManager());
束束= this.getIntent()getExtras()。
如果(NULL!=包)
{
ArrayList的< TabDetails> tabDetailsList =(ArrayList的< TabDetails>)bundle.getSerializable(GenericTabHost.TABS);
对于(TabDetails tabDetails:tabDetailsList)
{
host.addTab(host.newTabSpec(tabDetails.tabSpec)
.setIndicator(tabDetails.tabIndicator)
.setContent(新意图(getApplicationContext()
tabDetails.tabContent));
}
INT numChildren的= host.getTabWidget()getChildCount()。
的for(int i = 0; I< numChildren的;我++)
{
。host.getTabWidget()getChildAt(我).getLayoutParams()高度= 35。
}
}
}
其他
{
Log.e(GenericTabHost,####这个类必须在数据传递给自身打造####);
}
} //结束的onCreate
} //结束类
这个类的用户可以用它喜欢的:
的ArrayList< TabDetails> tabDetailsArray =新的ArrayList< TabDetails>(2);
tabDetailsArray.add(新TabDetails(tab_1中,
选项卡1,
SomeActivity.class));
tabDetailsArray.add(新TabDetails(Tab_2
选项卡2,
AnotherActivity.class));
意向意图=新的意图(getApplicationContext(),GenericTabHost.class);
intent.putExtra(GenericTabHost.TABS,tabDetailsArray);
startActivity(意向);
I've got a project in which I'm trying to do some refactoring of my existing TabHosts. I've got a bunch of very simple TabHost files that look like the class below. Some actually only have one tab, some 3, etc - so the only real difference in them is the number of tabs and the activity class loaded in each one. I'd like to just create a single TabHost that could get the info out of a passed in Bundle to determine how many tabs and the info needed (spec, indicator, content) to build/add each tab. It seems like the items I can place in the bundle are pretty basic and I'm not familiar with the Parcelable or Serializable features. Any suggestions?
public class SomeTabHost
extends ActivityGroup
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// Inflate ourselves into the layout ViewStub
ViewStub vs = (ViewStub) findViewById(R.id.theViewStub);
vs.setLayoutResource(R.layout.my_tabhost);
vs.inflate();
TabHost host = (TabHost) findViewById(android.R.id.tabhost);
host.setup(getLocalActivityManager());
host.addTab(host.newTabSpec("Tab1")
.setIndicator("Tab1")
.setContent(new Intent(this, SomeActivity.class)));
host.addTab(host.newTabSpec("Tab2")
.setIndicator("Tab2")
.setContent(new Intent(this, SomeOtherActivity.class)));
int numChildren = host.getTabWidget().getChildCount();
for ( int i=0; i <numChildren; i++ )
{
host.getTabWidget().getChildAt(i).getLayoutParams().height = 35;
}
}// end onCreate
}// end class
Looks like I jumped in asking this question too soon. I ended up solving my problem using a Serializable class implementation. Hopefuly somebody else finds this useful. See the code below:
First created class to hold the data
public class TabDetails implements Serializable
{
private static final long serialVersionUID = 1L;
public String tabSpec = "";
public String tabIndicator = "";
public Class<?> tabContent = null;
public TabDetails( String aTabSpec,
String aTabIndicator,
Class<?> aTabContent )
{
this.tabSpec = aTabSpec;
this.tabIndicator = aTabIndicator;
this.tabContent = aTabContent;
}
}//end class
Then updated the Generic Tab Host
public class GenericTabHost extends ActivityGroup
{
public static final String TABS = "TABS";
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// Inflate ourselves into the layout ViewStub
ViewStub vs = (ViewStub) findViewById(R.id.theViewStub);
vs.setLayoutResource(R.layout.mc_tabhost);
vs.inflate();
TabHost host = (TabHost) findViewById(android.R.id.tabhost);
host.setup(getLocalActivityManager());
Bundle bundle = this.getIntent().getExtras();
if ( null != bundle )
{
ArrayList<TabDetails> tabDetailsList = (ArrayList<TabDetails>) bundle.getSerializable(GenericTabHost.TABS);
for ( TabDetails tabDetails : tabDetailsList )
{
host.addTab(host.newTabSpec ( tabDetails.tabSpec )
.setIndicator( tabDetails.tabIndicator)
.setContent ( new Intent( getApplicationContext(),
tabDetails.tabContent ));
}
int numChildren = host.getTabWidget().getChildCount();
for ( int i=0; i <numChildren; i++ )
{
host.getTabWidget().getChildAt(i).getLayoutParams().height = 35;
}
}
}
else
{
Log.e("GenericTabHost", "#### This class must be passed in data to build itself ####");
}
}// end onCreate
}// end class
The user of this class can use it like:
ArrayList<TabDetails> tabDetailsArray = new ArrayList<TabDetails>(2);
tabDetailsArray.add(new TabDetails("Tab_1",
"Tab 1",
SomeActivity.class));
tabDetailsArray.add(new TabDetails("Tab_2",
"Tab 2",
AnotherActivity.class));
Intent intent = new Intent(getApplicationContext(), GenericTabHost.class);
intent.putExtra(GenericTabHost.TABS, tabDetailsArray);
startActivity(intent);
这篇关于Android的 - 创建一个通用TabHost,传递信息使用捆绑的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!