我使用TabHost并且我有2个选项卡,其中包含2个活动我想将第二个选项卡设置为默认选项卡但是当tabhost启动时他加载第一个tab1而在tab2之前这是错误的,因为在这两个活动中我从webservice加载数据! 我的问题是如何设置当前选项卡第二个选项卡而不加载第一个选项卡!
我的tabhost代码:
public class TabsHandler extends TabActivity {
private static TabHost tabHost;
Intent intent;
private void setupTabHost() {
tabHost = (TabHost) findViewById(android.R.id.tabhost);
tabHost.setup(this.getLocalActivityManager());
tabHost.setCurrentTab(1);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(!isOnline())
Utils.DialogNoInternet(this);
String authToken = getIntent().getExtras().getString("token");
Long role = getIntent().getExtras().getLong("role");
String EventIdNotification =getIntent().getExtras().getString("EventIdNotification");
setContentView(R.layout.event_list_tabs);
setupTabHost();
Resources res = getResources();
tabHost.getTabWidget().setClipChildren(false);
setupTab(new TextView(this), res.getString(R.string.tab1_List),new Intent().setClass(this, EventListActivity.class).putExtra("authToken", authToken).putExtra("role", role).putExtra("EventIdNotification", EventIdNotification));
setupTab(new TextView(this), res.getString(R.string.tab2_List),new Intent().setClass(this, PastEventListActivity.class).putExtra("authToken", authToken).putExtra("role", role).putExtra("isPast", "true"));
tabHost.setCurrentTab(1);
}
public void switchTab(int tab){
tabHost.setCurrentTab(tab);
}
public boolean isOnline() {
ConnectivityManager cm =
(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
return true;
}
return false;
}
private void setupTab(final View view, final String tag,Intent intent) {
View tabview = createTabView(tabHost.getContext(), tag);
TabSpec setContent = tabHost.newTabSpec(tag);
setContent.setIndicator(tabview);
setContent.setContent(intent);
tabHost.addTab(setContent);
}
private static View createTabView(final Context context, final String text) {
View view = LayoutInflater.from(context).inflate(R.layout.tabs_bg, null);
TextView tv = (TextView) view.findViewById(R.id.tabsText);
tv.setText(text);
return view;
}
答案 0 :(得分:1)
将tabHost.setCurrentTab(1);
替换为tabHost.setCurrentTab(2);
,它会将第二个标签设置为默认标签。
答案 1 :(得分:0)
public class TabsHandler extends TabActivity {
private static TabHost tabHost;
Intent intent;
private void setupTabHost() {
tabHost = (TabHost) findViewById(android.R.id.tabhost);
tabHost.setup(this.getLocalActivityManager());
tabHost.setCurrentTab(2);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(!isOnline())
Utils.DialogNoInternet(this);
String authToken = getIntent().getExtras().getString("token");
Long role = getIntent().getExtras().getLong("role");
String EventIdNotification =getIntent().getExtras().getString("EventIdNotification");
setContentView(R.layout.event_list_tabs);
setupTabHost();
Resources res = getResources();
tabHost.getTabWidget().setClipChildren(false);
setupTab(new TextView(this), res.getString(R.string.tab1_List),new Intent().setClass(this, EventListActivity.class).putExtra("authToken", authToken).putExtra("role", role).putExtra("EventIdNotification", EventIdNotification));
setupTab(new TextView(this), res.getString(R.string.tab2_List),new Intent().setClass(this, PastEventListActivity.class).putExtra("authToken", authToken).putExtra("role", role).putExtra("isPast", "true"));
tabHost.setCurrentTab(2);
}
public void switchTab(int tab){
tabHost.setCurrentTab(tab);
}
public boolean isOnline() {
ConnectivityManager cm =
(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
return true;
}
return false;
}
private void setupTab(final View view, final String tag,Intent intent) {
View tabview = createTabView(tabHost.getContext(), tag);
TabSpec setContent = tabHost.newTabSpec(tag);
setContent.setIndicator(tabview);
setContent.setContent(intent);
tabHost.addTab(setContent);
}
private static View createTabView(final Context context, final String text) {
View view = LayoutInflater.from(context).inflate(R.layout.tabs_bg, null);
TextView tv = (TextView) view.findViewById(R.id.tabsText);
tv.setText(text);
return view;
}