Tab总是调用第一个活动?

时间:2014-08-07 10:33:57

标签: android android-tabactivity

我在TabActivity中创建了MainActivity。我有5项活动。并将currentTab(2)设置为它。 我的意思是我想加载第三个标签作为启动活动。

问题

在第一个标签中,我在onCreate上调用AsyncTask。它正在调用,而我已将当前Tab设置为第三个活动。我尝试了不同的方法,但无法找到解决方案。你设置为currentTab的任何值。创建该值之前的所有选项卡。它可能是TabActivity的行为。

请指导我解决此类问题的方法。

public class MainActivity extends TabActivity implements OnTabChangeListener
{
TabHost tabHost;

// ImageView rate_img;

ImagesCache cache;

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

cache = ImagesCache.getInstance();

cache.initializeCache();

tabHost = getTabHost();

this.setDefaultTab(2);

/*rate_img = (ImageView) findViewById(R.id.rate_us);
rate_img.setOnClickListener(this);*/

// Tab for Highlights
TabSpec highlightsspec = tabHost.newTabSpec("Highlights");

highlightsspec.setIndicator("");

Intent highlightsIntent = new Intent(this,Highlights.class);

highlightsspec.setContent(highlightsIntent);

tabHost.addTab(highlightsspec);



// Tab for Live Score
TabSpec livescorespec = tabHost.newTabSpec("Live Score");

livescorespec.setIndicator("");

Intent livescoreIntent = new Intent(this,Livescore.class);

livescorespec.setContent(livescoreIntent);

tabHost.addTab(livescorespec);


//Tab for Live

TabSpec livespec = tabHost.newTabSpec("Live");

livespec.setIndicator("");

Intent liveIntent=new Intent(this,Live.class);

livespec.setContent(liveIntent);

tabHost.addTab(livespec);


// Tab for News

TabSpec newspec=tabHost.newTabSpec("News");

newspec.setIndicator("");

Intent newsIntent=new Intent(this,News.class);

newspec.setContent(newsIntent);

tabHost.addTab(newspec);

// Tab for Live Channels

TabSpec livechannelsTabSpec=tabHost.newTabSpec("Live Channels");

livechannelsTabSpec.setIndicator("");

Intent livechannelsIntent=new Intent(this,Livechannels.class);

livechannelsTabSpec.setContent(livechannelsIntent);

tabHost.addTab(livechannelsTabSpec);


//////////////////////////////Add Spaces To Tabs //////////////////////////////////

TabWidget tabWidget = (TabWidget) findViewById(android.R.id.tabs);
final int tabChildrenCount = tabWidget.getChildCount();
View currentView;
for (int i = 0; i < tabChildrenCount; i++)
{
currentView = tabWidget.getChildAt(i);
LinearLayout.LayoutParams currentLayout =
(LinearLayout.LayoutParams) currentView.getLayoutParams();
currentLayout.setMargins(0, 0, 0, 0);
}
tabWidget.requestLayout();

///////////////////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////////////////////////////////////////////

DisplayMetrics displaymetrics = new DisplayMetrics();

getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);

int height = displaymetrics.heightPixels;

int width = displaymetrics.widthPixels;


if((width <= 1600 && width > 800) || (height <= 2600 && height > 1280))
{
tabHost.getTabWidget().getChildAt(0).getLayoutParams().height = 230;

tabHost.getTabWidget().getChildAt(1).getLayoutParams().height = 230;

tabHost.getTabWidget().getChildAt(2).getLayoutParams().height = 230;

tabHost.getTabWidget().getChildAt(3).getLayoutParams().height = 230;

tabHost.getTabWidget().getChildAt(4).getLayoutParams().height = 230;
}
else
{
tabHost.getTabWidget().getChildAt(0).getLayoutParams().height = 140;

tabHost.getTabWidget().getChildAt(1).getLayoutParams().height = 140;

tabHost.getTabWidget().getChildAt(2).getLayoutParams().height = 140;

tabHost.getTabWidget().getChildAt(3).getLayoutParams().height = 140;

tabHost.getTabWidget().getChildAt(4).getLayoutParams().height = 140;
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////

tabHost.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.highlights_unselected);
tabHost.getTabWidget().getChildAt(1).setBackgroundResource(R.drawable.scores_unselected);
tabHost.getTabWidget().getChildAt(2).setBackgroundResource(R.drawable.live_selected);
tabHost.getTabWidget().getChildAt(3).setBackgroundResource(R.drawable.news_unselected);
tabHost.getTabWidget().getChildAt(4).setBackgroundResource(R.drawable.channels_unselected);


tabHost.setCurrentTab(2);

tabHost.setOnTabChangedListener(this);
}


@Override
public void onTabChanged(String tabId)
{

int index = tabHost.getCurrentTab();

if(index == 0)
{
tabHost.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.highlights_selected);
tabHost.getTabWidget().getChildAt(1).setBackgroundResource(R.drawable.scores_unselected);
tabHost.getTabWidget().getChildAt(2).setBackgroundResource(R.drawable.live_unselected);
tabHost.getTabWidget().getChildAt(3).setBackgroundResource(R.drawable.news_unselected);
tabHost.getTabWidget().getChildAt(4).setBackgroundResource(R.drawable.channels_unselected);
}
else if(index == 1)
{
tabHost.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.highlights_unselected);
tabHost.getTabWidget().getChildAt(1).setBackgroundResource(R.drawable.scores_selected);
tabHost.getTabWidget().getChildAt(2).setBackgroundResource(R.drawable.live_unselected);
tabHost.getTabWidget().getChildAt(3).setBackgroundResource(R.drawable.news_unselected);
tabHost.getTabWidget().getChildAt(4).setBackgroundResource(R.drawable.channels_unselected);
}
else if(index == 2)
{
tabHost.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.highlights_unselected);
tabHost.getTabWidget().getChildAt(1).setBackgroundResource(R.drawable.scores_unselected);
tabHost.getTabWidget().getChildAt(2).setBackgroundResource(R.drawable.live_selected);
tabHost.getTabWidget().getChildAt(3).setBackgroundResource(R.drawable.news_unselected);
tabHost.getTabWidget().getChildAt(4).setBackgroundResource(R.drawable.channels_unselected);
}

else if(index == 3)
{
tabHost.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.highlights_unselected);
tabHost.getTabWidget().getChildAt(1).setBackgroundResource(R.drawable.scores_unselected);
tabHost.getTabWidget().getChildAt(2).setBackgroundResource(R.drawable.live_unselected);
tabHost.getTabWidget().getChildAt(3).setBackgroundResource(R.drawable.news_selected);
tabHost.getTabWidget().getChildAt(4).setBackgroundResource(R.drawable.channels_unselected);
}

else if(index == 4)
{
tabHost.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.highlights_unselected);
tabHost.getTabWidget().getChildAt(1).setBackgroundResource(R.drawable.scores_unselected);
tabHost.getTabWidget().getChildAt(2).setBackgroundResource(R.drawable.live_unselected);
tabHost.getTabWidget().getChildAt(3).setBackgroundResource(R.drawable.news_unselected);
tabHost.getTabWidget().getChildAt(4).setBackgroundResource(R.drawable.channels_selected);
}

} }

1 个答案:

答案 0 :(得分:0)

首先,我想说的是,tab活动是从android中删除的,所以如果你使用片段,查看寻呼机,可跳转选项卡它会更好,它会给你更多的灵活性和控制选项卡相关的需求

使用tabhostOBJ.setCurrentTab(2);

tabActivity中的

方法,其中初始化了与标签相关的对象

2 是标签的索引编号,因此它会将当前标签设置为第三个活动 希望它会帮助你